0% found this document useful (0 votes)
3K views19 pages

Chapter 7 Classes and Objects

The document discusses classes and objects in C++. It defines key concepts like class, object, data member, member function, and access specifiers. It also provides examples of class definition syntax, declaring objects, and defining member functions inside and outside the class. Access specifiers like private, public, and protected are explained along with how they control access to class members.

Uploaded by

naseerkkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views19 pages

Chapter 7 Classes and Objects

The document discusses classes and objects in C++. It defines key concepts like class, object, data member, member function, and access specifiers. It also provides examples of class definition syntax, declaring objects, and defining member functions inside and outside the class. Access specifiers like private, public, and protected are explained along with how they control access to class members.

Uploaded by

naseerkkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CHAPTER 7 - CLASSES AND OBJECTS

CHAPTER 7 CLASSES AND OBJECTS

One mark questions:


1. What is an object? (K)
ANS: An object is a collection of data members and associated member functions.
2. What is a class? (K)
ANS: A class is a collection of objects that have identical properties, common behavior and
shared relationship.
3. What does the class definition specify? (S)
ANS: A class definition is a process of naming a class and data variables, and interface
operation of the class.
4. What does the class declaration specify? (S)
ANS: A class declaration specifies the representation of objects of the class and set of
operations that can be applied to such objects.
5. Why are access specifier used? (U)
ANS: Class is specified by three levels of access protection for hiding data and function
members internal to the class.
6. Mention any one access specifier? (U)
ANS: private
7. Which is the default access specifier? (U)
ANS: private
8. Which operator is used to access members of a class? (U)
ANS: (::) scope resolution operator.
9. Which type of data members can be accessed outside the class? (U)
ANS: Public members
10. What is the use/significance of scope resolution operator? (U)
ANS: To define member function outside the class declaration.
11. What is an array of objects? (U)
ANS: Collection of objects of same class (type).
12. Can an array be a member of a class? (U)
ANS: Yes
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 1
CHAPTER 7 - CLASSES AND OBJECTS

13. What are data members? (U)


ANS: The variables declared inside a class are known as data members.
14. What is the use of member-functions? (U)
ANS: Member-functions are used to interact with data contained within user defined type.
15. What are access specifiers? (U)
ANS: Every data member of a class is specified by three levels of access protection for
hiding data and function members internal to the class. They are Private , Public and
Protected.
16. How do you access a member function of a class? (U)
ANS: Using dot (.) operator
Syntax: ObjectName . memberFunctionName();
17. What are class instances called as? (U)
ANS: Objects
18. Write the syntax to declare an object of a class. (U)
ANS: Class_name object_name;
19. Give an example for object declaration. (U)
ANS: Student st;
Two marks questions:
1. What is the difference between class definition and class declaration? (U)
ANS: A class definition is a process of naming a class and data variables, and interface
operation of the class.
A class declaration specifies the representation of objects of the class and set of operations
that can be applied to such objects.
2. Write the syntax of class definition. (U)
ANS:

class className

private :

Data Member;

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 2


CHAPTER 7 - CLASSES AND OBJECTS

Member functions;

public :

Data Member;

Member functions;

protected :

Data Member ;

Member functions;

};
returnType className : : memberFunctionName ( arg1, arg2, ….argN)
{
function body;
}

3. Why are access specifiers used? Mention the different access specifiers. (U)
ANS: Class is specified by three levels of access protection for hiding data and function
members internal to the class.

4. How do you access the class member? Explain with syntax. (U)
ANS: The member of the class can be data or functions.
protected members of the class can be accessed only through the member
functions of the class.
The public data members of objects of a class can be accessed using direct member
access operator (.).
a) Syntax for accessing a data member of the class:
ObjectName . dataMember;
b) Syntax for accessing a member function of the class:

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 3


CHAPTER 7 - CLASSES AND OBJECTS

Object_Name . member_function(arguments)
5. Describe the significance of scope resolution operator. (U)
ANS: Scope resolution operator (::) is used to define the member function outside the
class.
6. How do you define a member function outside the class definition? Write the syntax. (U)
ANS: Using Scope resolution operator (::)
Syntax:
returnType className : : memberFunctionName ( arg1, arg2, ….argN)
{
function body;
}

7. Describe how to create an object with syntax and example. (U)


ANS: Syntax:
class ClassName
{
private : //Members
public : //Members
};
Class ClassName ObjectName1, ObjectName2,……;
Example: student st;
8. Explain with suitable example how arrays can be used as data member of a class. (U)
ANS:
class array
{
private:
int a[10]; // arrays as data member of a class
public:
void getdata ()
{
Cout<<”Enter 10 elements”;
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 4
CHAPTER 7 - CLASSES AND OBJECTS

For(i=0;i<10;i++)
Cin>>a[i];
}
};
9. Discuss private access specifier. (K)
ANS: private access means a member data can only be accessed by the class member
function or friend function.
private cannot be accessed from
outside the class.

private:
int x;
float y;
10. Explain public access specifier with example. (K)
ANS: public access means that member can be accessed any function inside or outside
the class.
public functions of a class provide interface for accessing the private and
protected members of the class.
11. Explain protected access specifier. (K)
ANS: The members which are declared using protected can be accessed only by the
member functions, friend of the class and also the member functions derived from this
class.
12. How are objects passed as an argument to a function? (A)
ANS:
o Copy of entire object is passed to function ( Pass by value)
o Only address of the object is transferred to the function (Pass by reference)
13. What are member functions? Name two methods to define member functions. (U)
ANS: Member functions are functions that are included within a class (Member functions
are also called Methods).

o Inside class definition


Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 5
CHAPTER 7 - CLASSES AND OBJECTS

o Outside class definition


14. Give an example to define a member function inside the class. (U)
ANS:
class rectangle
{
int length, breadth, area;
public:
void getData( ) // defining the member function inside the class
{
cout<< ” Enter the values for Length and Breadth”;
cin>>length>>breadth;
}
};
15. Give an example to define a member function outside the class. (U)
ANS:
class rectangle
{
int length, breadth, area;
public:
void get_data( );
};
Void rectangle :: getData( ) // defining the member function outside the class

{
cout<< ” Enter the values for Length and Breadth”;
cin>>length>>breadth;
}

16. Explain array of objects with an example. (U)


ANS: An array having class type elements is known as array of objects.

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 6


CHAPTER 7 - CLASSES AND OBJECTS

array.
class student
{
private:
int Name[10], rollno;
public:
void getdata ()
};
Student st[100]; //array of objects

17. How do you access the class members? Explain with syntax and example. (U)
ANS: The public data members of objects of a class can be accessed using direct member
access operator (.).
a) Syntax for accessing a data member of the class:
ObjectName . dataMember;
Example: st.rollno;
b) Syntax for accessing a member function of the class:
ObjectName . memberFunction(arguments)
Example: st.getdata( );
18. Write the characteristics of a member function defined outside the class. (K)
ANS:

the class with the name of member function.


the class name followed by two
colons (::) named scope resolution operator.
19. What is the significance of a class in OOP? (U)

ANS: Class is used in object-oriented programming to describe one or more objects.It


serves as a template for creating specific objects within a program.
20. How do you define an array of objects? (U)

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 7


CHAPTER 7 - CLASSES AND OBJECTS

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 8


CHAPTER 7 - CLASSES AND OBJECTS

array.
Example: Employee emp[3]; // Here ‘Employee’ is the name of a class
21. Explain how to define objects of a class with example. (U)
ANS: An object is a real world element which is identifiable entity with some characteristics
(attributes) and behavior (functions).
An object is an instance of a class. Objects are sometimes called as instance variables.
An object is normally defined in the main ( ) function.
Example :
class Student
{
private:
int rollno;
char name[20];
public:
void get_data( );
void display( );
};
Student S1, S2, S3;
//creation of objects

22. Write the difference between class definition and class declaration. (U)
ANS: Definition and Declaration of Classes:
class definition is a process of naming a class and data variables, and interface
operation of the class.
The variables declared inside a class are known as data members.
The functions declared inside a class are known as member functions.
class declaration specifies the representation of objects of the class and set of
operations that can be applied to such objects.
The general syntax of the class declaration is:

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 9


CHAPTER 7 - CLASSES AND OBJECTS

class className
{
private :
Data Member;
Member functions;
public :
Data Member;
Member functions;
protected :
Data Member ;
Member functions;
};
className objectName;

Five marks questions:


1. Explain the class definition with syntax and example. (U)
ANS: Declaration of Classes:
class definition is a process of naming a class and data variables, and interface
operation of the class.
The variables declared inside a class are known as data members.
The functions declared inside a class are known as member functions.
The general syntax of the class declaration is:

class ClassName
{
private :
Data Member;
Member functions;
public :
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 10
CHAPTER 7 - CLASSES AND OBJECTS

Data Member;
Member functions;
protected :
Data Member ;
Member functions;
};

2. Explain the access specifiers with example. (U)


ANS:

data and function members internal to the class.

private:
private access means a member data can only be accessed by the class member
function or friend function.
private cannot be accessed from
outside the class.

functions of the class. This property is also called information hiding.

private:
int x;
float y;
protected:
protected can be accessed only by the member
functions, friend of the class and also the member functions derived from this class.

protected access specifier is similar to private access specifiers.


public:

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 11


CHAPTER 7 - CLASSES AND OBJECTS

public access means that member can be accessed any function inside or outside the
class.
public functions of a class provide interface for accessing the private and
protected members of the class.

3. How do we define member functions? Explain any one with example. (U)
4. Explain how do we define member function inside the class definition. Give example. (U)
ANS: Member Function:

also called Methods).

o Inside class definition


o Outside class definition
Inside class definition:

replaced by actual function definition inside the class.

class rectangle
{
int length, breadth, area;

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 12


CHAPTER 7 - CLASSES AND OBJECTS

public:
void get_data( )
{
cout<< ” Enter the values for Length and Breadth”;
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
cout<<” The area of rectangle is”<<area;
}
}; //Class closing

5. Explain how do we define member function outside the class definition. Give example.
(U)
ANS: Outside class definition:
aration, you must link the class name of
the class with the name of member function.

colons (::).
::) are called scope resolution operator.
n operator (::) is used to define the member function outside the class.

returnType className : : memberFunctionName( arg1, arg2, ….argN)


{
function body;
}

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 13


CHAPTER 7 - CLASSES AND OBJECTS

class operation
{
private:
int a, b;
public:
int sum( );
int product( );
};
int operation : : sum( )
{
return (a+b);
}
int operation : : product( )
{
return (a * b);
}

6. What are member functions? Write the characteristics of member functions. (U)
ANS: Member Function:
ber functions are functions that are included within a class (Member functions are
also called Methods).
7. How do you create an object of a class? Explain with a programming example. (U)
ANS:
h some characteristics
(attributes) and behavior (functions).

follows:
class ClassName
{
private : //Members
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 14
CHAPTER 7 - CLASSES AND OBJECTS

public : //Members
};
class ClassName ObjectName1, ObjectName2,……;
// where class keyword is optional.

8. Explain arrays as member of the class with suitable programming example. (U)
ANS:
Array as member of classes:

This is illustrated in the following program.


#include<iostream.h>
#include<conio.h>
class array
{
private:
int a[10], m;
public:
void readarray( );
void displayarray( );
};
void array : : readarray( )
{
cout<<”Enter “<<m<<”Array 10 elements”<<endl;
for(int i=0; i<10; i++)
cin>> a[i];
}
void array : : displayarray( )
{
cout<<”Array elements are:”<<endl;
for(int i=0; i<10; i++)
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 15
CHAPTER 7 - CLASSES AND OBJECTS

cout<< a[i]<<”\t”;
}
void main( )
{
int n;
array a;
clrscr( );
a.readarray( );
a.dispalyarray( );
getch( );
}
OUTPUT:
Input number of elements: 5
Enter 5 Array elements
10 20 30 40 50
Array elements are:
10 20 30 40 50
9. What is array of objects? Explain with an example. (U)
ANS: Array of Objects:

nd is defined in the same way as any other


array.

class employee
{
private:
char name[10];
int age;
public:
void readdata( );
void displaydata( );
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 16
CHAPTER 7 - CLASSES AND OBJECTS

};
employee supervisor[3];
employee salesExecutive[5];
employee teamLeader[10];

ample, the array supervisor contains 3 objects namely supervisor[0],


supervisor[1], supervisor[2].

10. Describe how the objects can be used as function arguments. (U)
Objects as function arguments:

to any other data being sent as function argument.

o Copy of entire object is passed to function ( Pass by value)


o Only address of the object is transferred to the function (Pass by reference)
pass by value, copy of object is passed to the function.

#include<iostream.h>
#include<conio.h>
class exam
{
private:
float phy, che, mat ;
public:

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 17


CHAPTER 7 - CLASSES AND OBJECTS

void readdata( )
{
cout<<”Input Physics Marks” ;
cin>>phy;
}
void total(exam PU , exam CT)
{
phy = PU.phy + CT.phy;
}
void display( )
{
cout<< “Physics :” <<phy<<endl;
}
};
void main( );
{
Exam PUC, CET, PucCet;
clrscr( );
cout<<”Enter PUC Marks”<<endl;
PUC . readdata( );
cout<<”Enter CET Marks”<<endl;
CET . readdata( );
PucCet . total(PUC, CET);
cout<<”Total marks of PUC and CET is:” <<endl;
PucCet . display( );
}
OUTPUT:
Enter PUC Marks
Input Physics marks :
67
Enter CET Marks
Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 18
CHAPTER 7 - CLASSES AND OBJECTS

Input Physics marks :


60
Total marks of PUC and CET is:
Physics: 127

pass by reference, when an address of an object is passed to the function, the


function directly works on the original object used in function call.
function will reflect in the original
object, because the function is making changes in the original object itself.

object and not the entire object.


11. Difference between Structure and Classes:
ANS:
Structure Classes
A structure is defined with the struct
A class is defined with the class keyword
keyword
All the member of a structure are
All the members of a class are private by default
public by default
Structure cannot be inherited Class can be inherited
A structure contains only data A class contain both data member and member
member functions
Classes having data hiding features by using
There is no data hiding features access specifiers(public, private,protected)

Prepared by: Sanil Abraham , KMWA Deeksha Mahalakshmipuram Page 19

You might also like