Classes Objects
Classes Objects
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.
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);
}
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( );
}
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);
}
};
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.
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( );
}
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;
}
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);
}
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().
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.
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.
10
In the above example, a and b are the data members of the class Test. getnum() and
show_data() are member functions.
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.
Example:
class rectangle
{
int length;
int breadth ;
public:
void get_data( )
{
cin>>length;
cin>>breadth;
}
void put_data( )
{
cout<<length;
cout<<breadth;
}
};
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;
}
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);
}
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].
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