0% found this document useful (0 votes)
9 views

7. Classes-and-objects

Uploaded by

Irine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

7. Classes-and-objects

Uploaded by

Irine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CLASSES AND OBJECTS

ONE MARK QUESTIONS


1. What is a class?
Ans. A class is a collection of objects having similar characteristics.

2. What is an object?
Ans. An object is a basic real world entity with common behaviour and characteristics.

3. What are the two types of members referenced in a class?


Ans. The two types of members referenced in a class are data members and member
functions.

4. What are data members?


Ans. The variables which are declared in any class by using any fundamental data types
or derived data type are known as Data Members.

5. What is a member function?


Ans. Member function is the set of operations that are performed on the data members.

6. Mention the access specifier used with a class?


Ans. private, public and protected.

7. Is it possible to access data outside a class?


Ans. Yes, you can access data members and functions outside of the class by the help of
public access specifiers.

8. Which type of data members are accessible outside the class?


Ans. Public data members are accessible outside the class.

9. Which access specifier is implicitly used in a class?


Ans. Private access specifier.

10. Define the term public access.


Ans. Public access means that members can be accessed by any function outside the
class also.

11. Mention the operator used to access members of a class?


Ans. dot operator(.) is used to access members of a class.

12. What is the significance of scope resolution operator (::)?


Ans. Scope resolution operator is used to define member functions outside the class.

13. Which is the default access specifier for a members of a class in C++?
Ans. Private is the default access specifier for a member of a class in C++.
14. What is meant by array of objects?
Ans. An array having class type elements is known as array of objects.

15. Write the syntax to declare objects of a class?


Ans. classname object1, object2,…. , Objectn;

Two Marks Question //Three Marks Question:


1. Write the difference between class definition and class declaration?
Ans. Class definition:- Is a process of naming a class and data
variables, and interface operations of the class.
Class declaration:- Specifies the representation of objects of class and set of operations
applied on objects.

2. Write the syntax and example for class definition.


Ans .Syntax :
class classname
{
private: //members
public: //methods
};
Example :
class employee
{
private :
int empid;
char name[30];
float salary;
public:
void getdata( );
void putdata( );
};

3. Write the syntax and example for class declaration


Ans :Syntax :
class classname
{
private: //members
public: //methods
};
classname object1 , object2,….;
Example
class employee
{
private :
int empid;
char name[30];
float salary;
public:
void getdata( );
void putdata( );
};
employee e1 , e2; //e1 and e2 are objects of class
Employee

4. What is the significance of using access specifiers? Mention different access


specifiers.
Ans. The access specifier defines the scope of the data. The different access specifiers
are public, private and protected.

5. Discuss private access specifier with an example.


Ans. The data members and member function declared as private can be accessed only in that
class. If no access specifier is mentioned, then by default member are private. It is a default
access specifier.
EXAMPLE:
private:
int x;
float y;

6. Write short notes on public access specifier.


Ans. The data members declared under public can be accessed both inside and outside class
definition.
EXAMPLE:
public:
void input();
void output();

7. Explain protected access?


Ans. The members declared under protected can be accessed only by member functions
of same class, friend functions or derived class.
Example:
protected:
int a,b ;
int sum;

8. How are class members referenced? Discuss with suitable example.


Ans. The members of a class can be data or functions.
Private and protected members of a class can be accessed only through the member functions
of that class.
The member function of a class can be accessed only with an object of that class.
The public data members of objects of a class can be accessed using direct member access
operator (.) or dot operator.

SYNTAX for accessing DATA MEMBERS:


objectname . datamember_name;
Eg: S1. regno;

SYNTAX for accessing MEMBER FUNCTIONS:


objectname . member_function(arguments);
Eg: S1. getdata ( );

EXAMPLE PROGRAM
class sum
{
int a,b, result; data members
public: void inputdata();
void calculate(); member functions
void outputdata();
};
void main()
{
sum s; object creation
s . inputdata();
s . calculate(); Accessing members of a class
s . outputdata();
}

9. Discuss how objects of a class are referenced with an example.


Refer to Question No. 7 of FIVE mark question.

10. Differentiate private and public access specifier?


Ans. private means data members that can be accessed only inside the class.
Example:-
private:
int x;
float y;
public means data members that can be accessed both inside and
outside the class.
Example:-
public:
void input( );
void output( );

11. How can arrays be used as class members? Write an example.


Ans. Just like arrays within structures, it is possible to use arrays as member data of class type.

SYNTAX
class classname
{
private: datatype array_name[size];
};

EXAMPLE
class marks
{
int m[5]; //array as a member variable
int i ;
public: void input();
void output();
};

12. How are objects passed as arguments to a function? Give an example.


Ans. An object can be passed as arguments to a function in two ways
• Pass by value
• Pass by reference
Pass by value: the complete object is passed to a function as arguments.
Pass by reference: An address of the object is transferred to the function as arguments.
Syntax :
Object_name.functionname(object1 , object2, … , object n);
Example :
Obj.sum( obj1 , obj2);

Five Mark Questions :


1. Explain class definition and class declaration with syntax and example.
Ans. A class definition is a process of naming a class and data variables and
interface operations of the class.
The syntax of a class definition :
class classname
{
private :
data members;
member function;
public :
data members;
member function;
protected :
data members;
member functions;
};
A class declaration specifies the representation of objects of class and set of
operations applied on objects.
class classname
{
private: //members
public: //methods
};
classname object1 , object2,….;

class student
{
private:
int regno;
char name[25];
float fees;
public :
void getdata( );
void putdata( );
};
student s1,s2; //s1 and s2 are objects of class student

2. Describe access specifiers in a class.


Ans. Access specifier define the scope of the data.
The data members and member function can be accessed using access
specifiers.
The different types of access specifiers are private, public and protected.
Private : It is a default access specifier in C++
The members declared under private are accessible only inside
the class definition.
Example :
private :
int x;
int y;

Public: the data members declared under public can be accessed both inside and
outside class definition.
Example:
public:
void getdata( );
void compute( );
Protected: The members declared under protected dcan be accessed only by member
functions of same class, friend functions or derived class
Example
protected:
int a,b ;
int sum;

Example showing both private and public access specifiers


class account
{
private:
int accno;
char name[20];
public:
void getdata( );
void putdata( );
};

3. Explain member functions?


a) Inside class definition
b) Outside class definition
Ans. Inside class definition:
Function can be declared inside the class definition itself.
Syntax :
class classname
{
public :
void function( )
{
Statements;
}
};
Example :
class rectangle
{
int l,b;
public :
void getdata( )
{
cin>>l>>b;
}
void putdata( )
{
cout<<l<<b;
}
};
Outside class definition:
The scope resolution operator( :: ) is used to define member functions outside the
class definition
Syntax :
returntype classname : : memberfunction(arg1, arg2,….argn)
{
function body;
}
Example:
class rectangle
{
int l,b;
public :
void getdata( );
void putdata( );
};
void rectangle : : getdata( )
{
cin>>l>>b;
}
void rectangle : : putdata( )
{
cout<<l<<b;
}

4. What are the characteristics of member functions outside a class?


• Type and number of arguments in member function must be same as
type and number of data declared in class definition.
• The symbol : : (scope resolution operator) must be used along with the classname in
the header of function definition.
• Several classes can use same function name membership level will resolve their
scope.
• A non-member function cannot access private data of a class.

5. Explain how objects of a class can be defined?


Ans. Syntax:
class classname
{
private: //members;
public : // members;
};
classname object1, object2, ……………….;
example:
class student
{
private:
int regno;
float fees;
public:
void getdata();
}
student s1;

6. Illustrate how an array of objects can be defined.


Ans. An array having class type elements is known as array of objects.
Syntax
classname objectname[size];
Example
class data
{
int regno,marks;
public :
void getdata( );
void putdata( );
};
data stud[3];
In the above example the array stud contains three objects namely
stud[0] , stud[1] , stud[2].
Example program :
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class data
{
int rollno,marks;
public :
void getdata( );
void display( );
};
void data : : getdata( )
{
cout<<”Enter rollno”<<endl;
cin>>rollno;
cout<<”Enter marks”<<endl;
cin>>marks;
}
void data : : display( )
{
cout<<”Roll number= “<<rollno<<endl;
cout<<”Student marks= “<<marks<<endl;
}
void main( )
{
data stud[3];
clrscr( );
for(int i=0;i<3;i++)
{
stud[i]. getdata( );
stud[i].display( );
}
getch( );
}

7. Describe how objects can be used as function arguments.


Ans. An object can be passed as arguments to a function in two ways:
a) Pass by value.
b) Pass by reference.
Pass by value: the complete object is passed to a function argument.
Pass by reference: An address of object is transferred to function as arguments.

Syntax: objectname. functionname(object1, object2, …………….object n);


class sum
{
private: int n1, n2;
public: void input()
{
cout<<“enter n1 and n2 value”;
cin>>n1>>n2;
}
void output()
{
cout<<n1<“and”<<n2<<endl;
}
void add(sum s1,sum s2)
{
n1=s1.n1+s2.n1;
n2=s1.n2+s2.n2;
}
};
void main()
{
sum s1,s2,s3;
s1.input();
s2.input();
s3.add(s1,s2);
s3.output();
}

You might also like