Class and Object
Class and Object
#include<iostream.h>
int volume(double,int);
double volume( double , int );
double volume(longint ,int ,int);
main( )
{
cout<<volume(10)<<endl;
cout<<volume(10)<<endl; cout<<volume(10)<<endl;
}
int volume( ini s)
{
return (s*s*s); //cube
}
double volume( double r, int h)
{
return(3.1416*r*r*h); //cylinder
}
long volume (longint 1, int b, int h)
{
return(1*b*h); //cylinder
}
output:- 1000
157.2595
112500
35 P.T.O
Module-2:
LECTURE-13
CLASS:-
Class is a group of objects that share common properties and relationships .In C++, a class is
a new data type that contains member variables and member functions that operates on the variables.
A class is defined with the keyword class. It allows the data to be hidden, if necessary from external
use. When we defining a class, we are creating a new abstract data type that can be treated like any
other built in data type.
Generally a class specification has two parts:-
a) Class declaration
b) Class function definition
the class declaration describes the type and scope of its members. The class function
definition describes how the class functions are implemented.
Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only
from with in the class. On the other hand , public members can be accessed from outside the class
also. The data hiding is the key feature of oops. The use of keywords private is optional by default,
the members of a class are private.
The variables declared inside the class are known as data members and the functions
are known as members mid the functions. Only the member functions can have access to the private
data members and private functions. However, the public members can be accessed from the outside
the class. The binding of data and functions together into a single class type variable is referred to as
encapsulation.
Syntax:-
class item
{
int member;
float cost;
public:
void getldata (int a ,float b);
void putdata (void);
The class item contains two data members and two function members, the data
members are private by default while both the functions are public by declaration. The function
getdata() can be used to assign values to the member variables member and cost, and putdata() for
displaying their values . These functions provide the only access to the data members from outside
the class.
36 P.T.O
CREATING OBJECTS:
Once a class has been declared we can create variables of that type
by using the class name.
Example:
item x;
creates a variables x of type item. In C++, the class variables are known as objects. Therefore
x is called an object of type item.
37 P.T.O
LECTURE-14
Member function that are declared inside a class have to be defined separately
outside the class.Their definition are very much like the normal functions.
Syntax:
Example:
void item :: getdata (int a , float b )
{
number=a;
cost=b;
}
void item :: putdata ( void)
{
cout number=: number<<endl;
cout cost= cost<<endl;
}
The member function have some special characteristics that are often used in the program
development.
everal different classes can use the same function name. The "membership label"
will resolve their scope, member functions can access the private data of the class
.A non member function can't do so.
A member function can call another member function directly, without using the dot
operator.
38 P.T.O
INSIDE THE CLASS DEF1NATION:
Another method of defining a member function is to replace the function declaration by the
actual function definition inside the class .
Example:
class item
{
Intnumber;
float cost;
public:
void getdata (int a ,float b);
void putdata(void)
{
cout<<number<<endl; cout<<cost<<endl;
}
};
A C++ PROGRAM WITH CLASS:
# include< iostream. h>
class item
{
int number;
float cost;
public:
void getdata ( int a , float b);
void putdala ( void)
{
cout number: number<<endl;
cout cost : cost<<endl;
}
};
void item :: getdata (int a , float b)
{
number=a;
cost=b;
}
main ( )
{
item x;
cout \nobjectx endl;
x. getdata( 100,299.95);
x .putdata();
item y;
cout \n object y endl;
y. getdata(200,175.5);
y. putdata();
}
Output: object x
number 100
39 P.T.O
cost=299.950012
object -4
cost=175.5
Q.
Write a simple program using class in C++ to input subject mark and prints it.
ans:
class marks
{
private :
int ml,m2;
public:
void getdata();
void displaydata();
};
void marks: :getdata()
{
cout enter 1st subject mark: ;
cin>>ml;
cout enter 2nd subject mark: ;
cin>>m2;
}
void marks: :displaydata()
{
cout Ist subject mark: ml<<endl ;
cout 2nd subject mark: m2;
}
void main()
{
clrscr();
marks x;
x.getdata();
x.displaydata();
40 P.T.O
LECTURE-15
A member function can be called by using its name inside another member function of the
same class. This is known as nesting of member functions.
#include <iostream.h>
class set
{
int m,n;
public:
void input(void);
void display (void);
void largest(void);
};
int set::largest (void)
{
if(m>n)
return m;
else
return n;
}
void set::input(void)
{
cout input values of m and n: ;
cin>>m>>n;
}
void set::display(void)
{
cout largestvalue= largest() \n ;
}
void main()
{
set A;
A.input( );
A.display( );
}
output:
3017
largest value= 30
41 P.T.O
Private member functions:
Although it is a normal practice to place all the data items in a private section and all the functions in
public, some situations may require contain functions to be hidden from the outside calls. Tasks such
as deleting an account in a customer file or providing increment to and employee are events of
serious consequences and therefore the functions handling such tasks should have restricted access.
We can place these functions in the private section.
A private member function can only be called by another function that is a member of its class. Even
an object can not invoke a private function using the dot operator.
Class sample
{
int m;
void read (void);
void write (void);
};
if si is an object of sample, then
s.read();
is illegal. How ever the function read() can be called by the function update ( ) to
update the value of m.
void sample :: update(void)
{
read( );
}
42 P.T.O
#include<iostream.h>
class part
{
private:
int modelnum,partnum;
float cost;
public:
void setpart ( int mn, int pn ,float c)
{
modelmim=mn;
partnum=pn;
cost=e;
}
void showpart ( )
{
modelnum<<end1;
Cout num: partnum <<endl
Cout cost: $ <cost;
}
};
void main()
{
part pl,p2;
p1.setpart(644,73,217.55);
p2.setpart(567,89,789.55);
pl.showpart();
pl.showpart();
}
output:- model:644
num:73
cost: $217550003
model: 567
num:89
cost: $759.549988
43 P.T.O
#indude<iostream.h>
class distance
{
private:
int feet;
float inches;
public:
void setdist ( int ft, float in)
{
feet=ft;
inches=in;
}
void getdist()
{
cout enter feet: ;
cin>>feet;
cout enter inches: ;
cin>>inches;
}
void showdist()
{
cout<< feet inches«endl;
}
};
void main( )
{
distance dl,d2;
d1.setdist(1 1,6.25);
d2.getdata();
cout<<endl dist: d 1 .showdist();
cout \n dist2: ;
d2.showdist();
}
44 P.T.O
LECTURE-16
#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[20];
int age,sal;
public:
void getdata();
void putdata();
};
void employee : : getdata ()
{
cout enter name : ;
cin>>name;
cout enter age : ;
cin>>age;
cout enter salary:
cin>>sal;
return(0);
}
void employee : : putdata ( )
{
cout<<name <<endl;
cout<<age<<endl;
cout<<sal<<endl;
return(0);
}
int main()
{
45 P.T.O
employee emp[5]:
for( int i=0;i<5;i++)
{
emp[i].getdata();
}
cout<<endl;
for(i=0;i<5;i++)
{
emp[i].putdata();
}
getch();
return(0);
}
ARRAY OF OBJECTS:-
#include<iostream.h>
#include<conio.h>
class emp
{
private:
char name[20];
int age,sal;
public:
void getdata( );
void putdata( );
};
void emp : : getdata( )
{
coul enter empna : .
cin>>name;
cout enter age: ndl;
cin>>age;
cout enter salun : ;
46 P.T.O
cin>>sal;
}
void emp :: putdata ()
{
cout emp name: name<<endl;
cout emp age: age<<endl;
cout emp salary: sal;
}
void main()
{
emp foreman[5];
emp engineer[5];
for(int i=0;i<5;i ++)
{
cout for foreman: ;
foreman[i] . getdata();
}
cout<<endl;
for(i=0;i<5;i++)
{
Foreman[i].putdata(); .
}
for(int i=0;i<5;i ++)
{
cout for engineer: ;
ingineer[i].getdata();
}
for(i=0;i<5;i++)
{
ingineer[i].putdata();
}
getch();
return(0);
}
47 P.T.O
REPLACE AND SORT USING CLASS:-
#include<iostream.h>
#include<constream.h>
class sort
{
private:
int nm[30];
public;
void getdata();
void putdata();
}:
void sort :: getdata()
{
int i,j,k;
cout enter 10 nos: ;
for(i=0;i<10;i++)
{
cin>>nm[i];
}
for(i=0;i<9;i++)
{
for(j=i+l:j<10:j++)
{
if(nm[i]>nm[j])
{
k=nm[i];
nm[i]=nm[j];
nm[j]=k;
}
}
49 P.T.O
sum=sum+item_price[i];
}
cout<< \n total value: sum<<endl;
}
int main ( )
{
items order;
order.cnt();
int x;
do
{
cout \nyou can do the following: ;
cout enter appropriate no: ;
cout<<endl ;
cout<<endl 2: display total value : ;
cout<<endl 3 : display an item ;
cout<<endl 4 :display all item: ;
cout<<endl 5 : quit: ;
cout<<endl<<endl what is your option: ;
cin>>x;
switch(x)
{
case 1: order.get_item(); break;
case 2: order.display_sum(); break;
cose 3: order.remove(); break;
case 4: order.display_item();break;
case 5: break;
default : cout error in input; try again ;
}
} while(x!=5);
}
50 P.T.O
LECTURE-17
Note that the type and scope of each static member variable must be defined outside
the class definition .This is necessary because the static data members are stored separately rather
than as a part of an object.
Example :-
#include<iostream.h>
class item
{
static int count; //count is static
int number;
public:
void getdata(int a)
. {
number=a;
count++;
}
void getcount(void)
{
cout count: ;
cout<<count<<endl;
}
};
int item :: count ; //count defined
int main( )
{
item a,b,c;
a.get_count( );
b.get_count( );
c.get_count( ):
a.getdata( ):
b.getdata( );
51 P.T.O
c.getdata( );
cout«"after reading data : "«endl;
a.get_count( );
b.gel_count( );
c.get count( );
return(0);
}
The static Variable count is initialized to Zero when the objects created . The count is
incremented whenever the data is read into an object. Since the data is read into objects three times
the variable count is incremented three times. Because there is only one copy of count shared by all
the three object, all the three output statements cause the value 3 to be displayed.
52 P.T.O
test t1,t2;
t1.setcode( );
t2.setcode( );
test :: showcount ( ); '
test t3;
t3.setcode( );
test:: showcount( );
t1.showcode( ) ;
t2.showcode( );
t3.showcode( );
return(0);
output:- count : 2
count: 3
object number 1
object number 2
object number 3
Like any other data type, an object may be used as A function argument. This can cone in two ways
1. A copy of the entire object is passed to the function.
2. Only the address of the object is transferred to the function
The first method is called pass-by-value. Since a copy of the object is passed to the function, any
change made to the object inside the function do not effect the object used to call the function.
The second method is called pass-by-reference . When an address of the object is passed, the called
function works directly on the actual object used in the call. This means that any changes made to the
object inside the functions will reflect in the actual object .The pass by reference method is more
efficient since it requires to pass only the address of the object and not the entire object.
Example:-
#include<iostream.h>
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout<< hours hours and: ;
53 P.T.O
void sum( time ,time);
};
void time :: sum (time t1,time t2) .
{
minutes=t1.minutes + t2.minutes;
hours=minutes%60;
minutes=minutes%60;
hours=hours+t 1.hours+t2.hours;
}
int main()
{
time T1,T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout T1= ;
T1.puttime( );
cout T2= ;
T2.puttime( );
cout T3= ;
T3.puttime( );
return(0);
}
54 P.T.O