Chapter 7 Classes and Objects
Chapter 7 Classes and Objects
class className
private :
Data Member;
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:
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;
}
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).
{
cout<< ” Enter the values for Length and Breadth”;
cin>>length>>breadth;
}
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:
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:
class className
{
private :
Data Member;
Member functions;
public :
Data Member;
Member functions;
protected :
Data Member ;
Member functions;
};
className objectName;
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;
};
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.
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.
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:
class rectangle
{
int length, breadth, area;
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.
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:
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:
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];
10. Describe how the objects can be used as function arguments. (U)
Objects as function arguments:
#include<iostream.h>
#include<conio.h>
class exam
{
private:
float phy, che, mat ;
public:
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