Professional Documents
Culture Documents
Class declaration
class class_name
{
private:
variables declarations;
functions declarations;
public:
variables declarations;
functions declarations;
};
Class
private area
No Data
Entry in
private
Functions
X
Data
Entry
allowed Functions
in public
public area
class Bowler
{
int runsGiven;
float overs;
public:
void readValue(int rg, float ov );
float economyRate( );
};
void Bowler :: readValue(int rg, float ov)
{
runsGiven = rg;
overs = ov;
}
float Bowler :: economyRate( )
{
return(runsGiven/overs);
}
class Reader
{
int val;
void read();
public:
void update();
void write();
};
Reader rd; // rd is object
then
#include<iostream.h>
class Sorter
{
int num[10];
public:
void input( );
void sort( );
void largest( );
};
void Sorter::input( )
{
for(int x=0;x<10;x++) cin>>num[x];
}
void Sorter::sort( )
{
for(int x=0;x<10;x++)
for(int y=x;y<10;y++)
if(num[x]>num[y])
{
num[x] = num[x] + num[y];
num[y] = num[x] - num[y];
num[x] = num[x] - num[y];
}
for(x=0;x<10;x++)
cout<<"\n"<<num[x];
}
void Sorter::largest( )
{
cout<<"\nLargest number:"<<num[9];
}
int main( )
{
Sorter sobj;
sobj.input( );
sobj.sort( );
sobj.largest( );
return(0);
}
Data members
Memory
created when
Member functions functions
defined
Objects in Memory
count
C haracteristics:
1. It is initialized to zero when the first
object of its class is created. No other
initialization is permitted.
2. Only one copy of that member is
created and shared by all the objects of
the class.
3. It is visible only within the class but its
lifetime is the entire program.
Array of objects
class Student
{
char name[20];
float marks;
public:
void accept( );
void print( );
};
void Student :: accept( )
{
cout<<"\nEnter name: ";
cin>>name;
cout<<"\nEnter marks: ";
cin>>marks;
}
void Student :: print( )
{
cout<<"\nName: "<<name;
cout<<"\nMarks: "<<marks;
}
int main( )
{
Student SYIT[5];
for(int i=0;i<5;i++)
SYIT[i].accept( );
cout<<"\n\nYour data:\n";
for(i=0;i<5;i++)
SYIT[i].print( );
return(0);
}
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 11 -
Chapter 02 Class and Objects
name accept( )
SYIT[0]
marks print( )
name accept( )
SYIT[1]
marks print( )
name accept( )
SYIT[2]
marks print( )
name accept( )
SYIT[3]
marks print( )
name accept( )
SYIT[4]
marks print( )
class Square
{
public:
int number;
void input(int);
int largeNum(Square, Square);
};
void Square::input(int num)
{
number = num;
}
int Square::largeNum(Square s, Square t)
{
if(s.number > t.number)
return(s.number);
return(t.number);
}
int main( )
{
Square s1,s2,s3;
s1.input(45);
s2.input(29);
cout<<"Large:"<<s3.largeNum(s1,s2);
return(0);
}
class Time
{
int hrs;
int min;
public:
void getTime(int h, int m)
{
hrs = h; min = m;
}
void putTime( )
{
cout<<hrs<<" hrs and ";
cout<<min<<" minutes\n";
}
void addTime(Time t1, Time t2)
{
min = t1.min + t2.min;
hrs = min / 60;
min = min % 60;
hrs = hrs + t1.hrs + t2.hrs;
}
};
int main( )
{
Time x, y, z;
x.getTime(2, 45);
y.getTime(3, 30);
z.addTime(x, y);
cout<<"Addition:";
z.putTime( );
return(0);
}
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 14 -
Chapter 02 Class and Objects
15 45 30
min x.min y.min
(x + y)
z.addTime(x, y)
class MyClass
{
int num1, num2;
public:
void initialize( );
int average(MyClass s);
};
void MyClass::initialize( )
{
num1 = 10;
num2 = 20;
}
int average(MyClass s)
{
return((s.num1+s.num2)/2);
}
int main()
{
MyClass x;
x.initialize();
cout<<"Average:"<<average(x);
return(0);
}
class Slave;
class Master
{
int x;
public:
void initialize(int i)
{
x = i;
}
friend void findMax(Master, Slave);
};
class Slave
{
int y;
public:
void initialize(int j)
{
y = j;
}
friend void findMax(Master, Slave);
};
void findMax(Master m, Slave s)
{
if(m.x > s.y) cout<<"Max:"<<m.x;
else cout<<"Max:"<<s.y;
}
int main( )
{
Master a;
Slave b;
a.initialize(13);
b.initialize(19);
findMax(a,b);
return 0;
}
Operator Overloading
#include<iostream.h>
class Space
{
int x,y;
public:
void get(int a, int b)
{
x = a;
y = b;
}
void show()
{
cout<<"\n X = "<<x;
cout<<"\n Y = "<<y;
}
void operator–()
{
x = –x;
y = –y;
}
};
int main()
{
Space s;
s.get(10,52);
cout<<"\nBefore overloading:";
s.show();
cout<<"\nAfter overloading:";
–s;
s.show();
return(0);
}
#include<iostream.h>
class Index
{
int val;
public:
Index(){}
Index(int z)
{
val = z;
}
void show()
{
cout<<"\n val = "<<val;
}
void operator++()
{
val = val + 10;
}
};
int main()
{
Index sit(9);
cout<<"\nBefore overloading:";
sit.show();
cout<<"\nAfter overloading:";
++sit;
sit.show();
return(0);
}
class Overload
{
int n1,n2,n3;
public:
Overload(){}
Overload(int a,int b,int c)
{
n1 = a; n2 = b; n3 = c;
}
void show()
{
cout<<"\n n1 = "<<n1;
cout<<"\n n2 = "<<n2;
cout<<"\n n3 = "<<n3;
}
v o i d o p e r a t o r *()
{
n1 = n2 = n3 = 0;
}
};
int main()
{
Overload check(6,3,4);
cout<<"\nBefore overloading:";
check.show();
cout<<"\nAfter overloading:";
*check;
check.show();
return(0);
}