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

Classes Objects

Uploaded by

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

Classes Objects

Uploaded by

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

Ch.

7 Classes and Objects


One mark questions:
1. What is a class?
A. Classes are the fundamental building blocks in OOP. A class is a user defined data type
which combines both data elements and operations associated with the data.

2. What is an object?
A. An object is a collection of data members and associated member functions. An object is
an instance of a class.

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


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

4. What are data members?


A. Data members are used to describe the characteristics or properties of the object under
consideration.

5. What is a member function?


A. Member functions are the set of operations that are performed on the objects of the
class.

6. Mention the access specifiers used with a class.


A. The access specifiers used with a class are private, public and protected.

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


A. Yes, it is possible to access data outside a class, provided the data is declared in the public
access specifier.

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


A. The data members declared under public access specifier are accessible outside a class.

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


A. Private access specifier is implicitly used in a class.

10. Define the term public access.


A. The members defined under public access specifier can be accessed inside as well as
outside class definition.

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


A. The member access operator dot(.) is used to access members of a class.

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


A. The scope resolution operator identifies the function as a member of particular class.

1
13. How are objects of a class declared? Give an example.
A. The objects of a class are declared in the same manner like any other variable
declaration.
Syntax:
class user_defined_name
{
private: //members
public: //methods
};
user_defined_name object1,object2,…;
Example:
class num
{
public:
int sum(int p,int q);
int diff(int p,int q);
};
void main( )
{
num s1,s2; //s1 , s2 are objects of class num
s1.sum(200,300);
s2.diff(600,500);
}

14. What is meant by an array of objects?


A. An array having class type elements is known as an array of objects.

15. Write an example to show how objects can be used as function arguments?
A.
#include<iostream.h>
class rup
{
private:
int n1,n2;
public:
void get( )
{
cin>>n1>>n2;
}
void print( )
{
cout<<n1<<n2;
}
void multi(rup r1, rup r2)
{
n1=r1.n1*r1.n2;

2
n2=r2.n1*r2.n2;
}
};
void main()
{
rup r1,r2,r3;
r1.get( );
r2.get( );
r3.multi(r1,r2); //objects are passed as arguments to the function
r3.print( );
}

Two/Three mark questions:


1. Write the differences between class definition and class declaration.
A. A class definition is a process of naming a class, data variables and interface
operations 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 and example for class definition.


A. Syntax:
class user_defined_name
{
private:
//Member data
//Member functions
protected:
//Member data
//Member functions
public:
//Member data
//Member functions
};

Example:
class Test
{
private:
int a,b;
public:
void getnum( )
{
cout<<”Enter Numbers for Addition:”;
cin>>a>>b;
}
void show_data( )
{

3
cout<<”Addition of two numbers:”<<(a+b);
}
};

3. Write the syntax and example for class declaration.


A. Syntax:
class user_defined_name
{
private:
//Member data
//Member functions
protected:
//Member data
//Member functions
public:
//Member data
//Member functions
};

void main()
{
user_defined_name obj1, obj2,…objn;
}
Example:
#include<iostream.h>
#include<conio.h>
class Test
{
private:
int a,b;
public:
void getnum( )
{
cout<<”Enter Numbers for Addition:”;
cin>>a>>b;
}
void show_data()
{
cout<<”Addition of two numbers:”<<(a+b);
}
};

void main( )
{
Test a1;
a1.getnum( );
a1.show_data( );

4
}

4. What is the significance of using access specifiers? Mention different access specifiers.
A. Every data member of a class is specified by three levels of access protection for hiding
data and function members internal to the class. The access specifiers define the scope of
data. The different access specifiers are private, public and protected.

5. Discuss private access specifiers with an example.


A. Private access means a member data can only be accessed by the member function.
Members declared under private are accessible only within the class. If no access specifier is
mentioned, then by default, members are private.
Example:
class box
{
private:
int height;
public:
int length, width;
void set_height(int i)
{
height=i;
}
int get_height( )
{
return height;
}
};
void main( )
{
box object;
//public variables can be accessed directly using object name
object.length=10;
object.width=20;
//private variable can be accessed only through its public method
object.set_height(30);
object.get_height( );
}

6. Write short notes on public specifiers.


A. Public access means that members can be accessed by any function inside and outside
the class.

Example:
class box
{
private:
int height;

5
public:
int length, width;
void set_height(int i)
{
height=i;
}
int get_height( )
{
return height;
}
};
void main( )
{
box object;
//public variables can be accessed directly using object name
object.length=10;
object.width=20;
//private variable can be accessed only through its public method
object.set_height(30);
object.get_height( );
}

7. Explain protected access.


A. The members which are declared using protected can be accessed only by the member
functions, friend functions of the class and also by the member functions derived from this
class. The members cannot be accessed from outside. The protected access specifier is
therefore similar to private specifier.

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


A. 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. No function outside
a class can include statements to access data directly. The public data members of objects of
a class can be accessed using direct member access operator(.)
The syntax for accessing class member is
class-object.member-data;
class-object.member-function(arguments);

Example:
class box
{
private:
int height;
public:
int length, width;
void set_height(int i)
{

6
height=i;
}
int get_height( )
{
return height;
}
};
void main( )
{
box object;
//public variables can be accessed directly using object name
object.length=10;
object.width=20;
//private variable can be accessed only through its public method
object.set_height(30);
object.get_height( );
}

9. What is meant by referencing member functions inside class definition and outside class
definition?
A. Inside class definition:
To define member function inside a class, the function declaration within the class is
replaced by actual function definition inside the class. A function defined in a class is treated
as inline function. Only small functions are defined inside class definition.
Syntax:
return_type memberfunction(arg 1, arg 2,…,arg n)
{
// function body
}
Outside class definition:
Member functions declared within a class must be defined separately outside the
class. The definition of member function is similar to normal function. But a member
function has an ‘identity label’ in the header. This label tells the compiler which class the
function belongs to. Scope resolution operator :: is used to define the member function.
Syntax:
return_type classname :: member function(arg 1, arg 2, …, arg n)
{
//function body;
}

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


A. When a class is defined, it specifies the type information the objects of the class will
store. Once the class is defined an object is created from that class. The objects of a class
are declared in the same manner like any other variable declaration.

Syntax:
class user_defined_name

7
{
private: //members
public: //methods
};
User_defined_name object1, object2,…;
Example:
class num
{
public:
int sum(int p, intq);
int diff(int p, intq);
};
void main( )
{
num s1, s2; //s1 and s2 are objects of class num
s1.sum(200,300);
s2.diff(600,500);
}

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


A. Just like arrays within structures, it is possible to use arrays as member data of class type.
Example:
class marks
{
int m[5], i;
public:
void setval();
void display();
};
void marks::setval( )
{
cout<<”Enter marks:”;
for(i=0;i<5;i++)
cin>>m[i];
}
void marks::display( )
{
cout<<”The marks are:”;
for(i=0;i<5;i++)
cout<<m[i];
}
In the above example, the array variable m is a private member of class marks. It is used by
the member functions setval( ) and display( ).

8
12. How are objects passed as arguments to a function? Give an example.
A. An object can be passed to a function in two ways:
 Copy of entire object is passed to the function (pass-by-value)
 Only address of the object is transferred to the function (pass-by-reference)
Example:
#include<iostream.h>
class rup
{
private:
int n1,n2;
public:
void get()
{
cin>>n1>>n2;
}
void print()
{
cout<<n1<<n2;
}
void multi(rup r1, rup r2)
{
n1=r1.n1*r1.n2;
n2=r2.n1*r2.n2;
}
};
void main( )
{
rup r1,r2,r3;
r1.get( );
r2.get( );
r3.multi(r1,r2);
r3.print( );
}
In the above example, objects r1 and r2 are passed as arguments to the member function
multi().

Five mark questions:

1. Explain class definition and class declaration with syntax and example.
A. A class definition is a process of naming a class and data variables, and interface
operations 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.

The definition and declaration of a class indicates the following:

9
 The data variables known as member data of a class, describe the characteristics of a
class.
 Member functions are the set of operations that are performed on the objects of the
class.
 The access control specifiers to the class members within a program are specified.
 Class tagname for external operations for accessing and manipulating the instance of
a class.

Syntax for defining a class:


class user_defined_name
{
private:
//Member data
//Member functions
protected:
//Member data
//Member functions
public:
//Member data
//Member functions
};
Keyword class is used to declare a class. User_defined_name is the name of the class.
Example:
#include<iostream.h>
class Test
{
private:
int a,b;
public:
void getnum() //input data values
{
cout<<”Enter Numbers for Addition:”;
cin>>a>>b;
}
void show_data() //add two numbers and display result
{
cout<<”Addition of two numbers:”<<(a+b);
}
};
void main( )
{
Test a1;
a1.getnum( );
a1.show_data( );
}

10
In the above example, a and b are the data members of the class Test. getnum() and
show_data() are member functions.

2. Describe access specifiers in a class.


A. Every data member of a class is specified by three levels of access protection for hiding
data and function members internal to the class. The access specifiers define the scope of
data. The access specifiers are indicated using the following key words:
 private
 protected
 public
private:
private access means a member data can only be accessed by the member function.
Members declared under private are accessible only within the class. If no access specifier is
mentioned, then by default, members are private.
Example:
private:
int x;
float y;

public:
public access means that members can be accessed by any function inside and
outside the class also.

Example:
class box
{
private:
int height;
public:
int length, width;
void set_height(int i)
{
height=i;
}
int get_height( )
{
return height;
}
};
void main( )
{
box object;
//public variables can be accessed directly using object name
object.length=10;
object.width=20;
//private variable can be accessed only through its public method

11
object.set_height(30);
object.get_height( );
}

protected:
The members which are declared using protected can be accessed only by the
member functions, friends of the class and also by the member functions derived from this
class.

3. Explain member functions


(a) Inside class definition (b) Outside class definition

A. (a) Inside class definition:


To define member function inside a class, the function declaration within the class is
replaced by actual function definition inside the class. A function defined in a class is treated
as inline function. Only small functions are defined inside class definition.
Syntax:
return_type memberfunction(arg 1, arg 2,…,arg n)
{
// function body
}

Example:
class rectangle
{
int length;
int breadth ;
public:
void get_data( )
{
cin>>length;
cin>>breadth;
}
void put_data( )
{
cout<<length;
cout<<breadth;
}
};

(b) Outside class definition:


Member functions declared within a class must be defined separately outside the
class. The definition of member function is similar to normal function. But a member
function has an ‘identity label’ in the header. This label tells the compiler which class the
function belongs to. Scope resolution operator :: is used to define the member function.

12
Syntax:
return_type classname :: member function(arg1, arg2, …, argn)
{
//function body;
}
Example:
class rectangle
{
int length;
int breadth ;
public:
void get_data( );
void put_data( );
};
void rectangle::get_data( )
{
cin>>length;
cin>>breadth;
}
void rectangle::put_data( )
{
cout<<length;
cout<<breadth;
}

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


A.
 Type and number of arguments in member function must be same as the type and
number of data declared in class definition.
 The symbol :: is known as scope resolution operator. Usage of :: along with class
name is the header of function definition. The scope resolution operator identifies
the function as a member of particular class.
 Several classes can use same function name. Membership label will resolve their
scope.
 Member function can access private data of a class. A non-member function cannot.

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


A. When a class is defined, it specifies the type information of the objects of the class store.
Once the class is defined an object is created from that class. The objects of a class are
declared in the same manner like any other variable declaration.

13
Syntax:
class user_defined_name
{
private: //members
public: //methods
};
user_defined_name object1, object2,….;
Example:
class num
{
private:
int x, y;
public:
int sum(int p, int q);
int diff(int p, int q);
};
void main( )
{
num s1, s2; //s1 and s2 are objects of class num
s1.sum(200,300);
s2.diff(600,500);
}

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

A. An array having class type elements is known as array of objects. An array of objects is declared
after class definition and is defined in the same way as any other array.

Example:

#include<iostream.h>
class employee
{
private:
char name[10];
int age;
public:

void getdata( )
{
cout<<”Enter the name of the employee:”;
cin>>name;
cout<<”Enter the age:”;

14
cin>>age;
}
void dispdata( )
{
cout<<”Name:”<<name;
cout<<”Age:”<<age;
}
};
void main( )
{
employee supervisor[3];
int i;
for( i=0;i<3;i++)
supervisor[i].getdata( );
for( i=0;i<3;i++)
supervisor[i].dispdata( );
}

In the above example, the array supervisor contains 3 objects namely supervisor[0],
supervisor[1], supervisor[2].

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


A. An object can be passed to a function in two ways:
 Copy of entire object is passed to function (pass-by-value)-The function
creates its own copy of the object and uses it. Therefore changes made to the
object inside the function do not affect the original object.
 Only address of the object is transferred to the function (pass-by-reference)-
The function directly works on the original object used in function call.
Therefore changes made to the object inside the function will reflect in the
original object, because the function is making changes to the original object
itself.
Example:
#include<iostream.h>
class rup
{
private:
int n1,n2;
public:
void get()
{
cin>>n1>>n2;
}
void print()
{
cout<<n1<<n2;
}

15
void multi(rup r1, rup r2)
{
n1=r1.n1*r1.n2;
n2=r2.n1*r2.n2;
}
};
void main( )
{
rup r1,r2,r3;
r1.get( );
r2.get( );
r3.multi(r1,r2);
r3.print( );
}
In the above example, objects r1 and r2 are passed as arguments to the member function
multi().

8. Let product list be a linear array of size N where each element of the array contains
following fields Item code, price and quantity. Declare a class product list with the three
data members and member functions to perform the following:
a. Add values to the product list.
b. Printing the total stock value.
A.
#include <iostream.h>
class product_list
{
private:
int itemcode, qty;
double price;
public:
void get( )
{
cout<<”Enter the item code of a product:”;
cin>>itemcode;
cout<<”Enter the quantity:”;
cin>>qty;
cout<<”Enter the price:”;
cin>>price;
}
void print( )
{
cout<<”Item code:”<<itemcode;
cout<<”Stock value:”<<(qty*price);
}
};
void main( )
{

16
product_list p[100];
int n,i;
cout<<”Enter the number of products:”;
cin>>n;
for(i=0;i<n;i++)
p[i].get( );
for(i=0;i<n;i++)
p[i].print( );

9. A class clock has following members a.hour b. minute. Create member functions
a. To initialize the data members
b. Display the time
c. To convert hours and minutes to minutes
A.
#include <iostream.h>
class clock
{
private:
int hour, minute;
public:
void getdata( ) //assign values to data members
{
cout<<”Enter the hours:”;
cin>>hour;
cout<<”Enter the minutes:”;
cin>>minute;
}
void caldata( ) //convert hours and minutes to minutes
{
minute=minute+hour*60;
}
void display( ) //display the time
{
cout<<”Total minutes=”<<minute;
}
};
void main( )
{
clock c;
c.getdata();
c.caldata();
c.display();
}

17
10. Write a program that receives arrival time, departure time and speed of an
automobile in kilometres/hour as input to a class. Compute the distance travelled in
meters/second and display the result using member functions.

#include<iostream.h>
class Time
{
private:
float arr_time, dep_time, kmphspeed, mpsspeed, time_hr, dist_km;
public:
void get()
{
cout<<”Enter the departure time and arrival time in hours:”;
cin>>dep_time>>arr_time;
cout<<”Enter the speed of the automobile in km/hr:”;
cin>>kmphspeed;
}
void compute()
{
time_hr=arr_time-dep_time;
dist_km=time_hr*kmphspeed;
mpsspeed=(kmphspeed*10)/36;

}
void display()
{
cout<<”Distance travelled in Kms:”<<dist_km;
cout<<”Time taken:”<<time_hr;
cout<<”Speed in kmph:”<<kmphspeed;
cout<<”Speed in mps:”<<mpsspeed;
}
};
void main()
{
Time t;
t.get();
t.compute();
t.display();
}

18

You might also like