Object Oriented Programming Pointer in C++
Object Oriented Programming Pointer in C++
r2.displaydata( );
r2.area_peri( );
r3.getdata( );
r3.displaydata( );
r3.area_peri( );
}
array[i].print_data();
}
Example 3 :
#include<iostream.h>
class Account
{
int acc_no;
float amount;
char acc_type;
public:
Account(){}
Account(int no,float bal,char type='S')
{
acc_no=no;
amount=bal;
acc_type=type;
}
void display()
{
cout<<"\nAccount No. : "<<acc_no;
cout<<"\nBalance : "<<amount;
cout<<"\nAccount Type : "<<acc_type;
}
};
void main()
{
Account a1,a2;
int no;
float bal;
char type;
cout<<"\nEnter Account Number, Balance and Account type";
cin>>no>>bal>>type;
a1=Account(no,bal,type);
cout<<"\nEnter Account Number, Balance";
cin>>no>>bal;
a2=Account(no,bal);
a1.display();
a2.display();
}
Example 4 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
{
private:
char name[20],type[20];
int acno,bal,wit,dep;
public:
bank()
{
strcpy(name,"Rina");
acno=5;
bal=500;
strcpy(type,"Saving");
}
void deposit()
{
cout<<"\nEnter amt to deposit :";
cin>>dep;
bal=bal+dep;
show();
}
void withdraw()
{
cout<<"\nEnter amt to withdraw :";
cin>>wit;
if(bal-wit>=500)
bal=bal-wit;
else
cout<<"\nYou cannot withdraw";
show();
}
void show()
{
cout<<"\nDepositor name :"<<name<<endl;
cout<<"\nBalance :"<<bal<<endl;
}
};
void main()
{
bank bk;
clrscr();
bk.deposit();
bk.withdraw();
getch();
}
clrscr();
sample A(5,10), B(5,5), C;
cout << "The result of the addition is :\n";
C = A + B;
// invocation of the operator function
C.print();
cout << "\nSecond method of invocation of operator function-\n";
cout << "The result of the addition is :\n";
C = operator + (A,B); // different way of invocation
C.print();
{
sample temp;
temp.a = a + x.a;
temp.b = b + x.b;
return temp;
}
void sample::print()
{
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
sample A(5,10), B(5,5), C;
cout << "The result of the addition is :\n";
C = A + B;
// invocation of the operator function
C.print();
cout << "\nSecond method of invocation of operator function- \n";
cout << "The result of the addition is :\n";
C = A.operator + (B); // different way of invocation
C.print();
}
Example 8 :
#include<iostream.h>
#include<conio.h>
class book
{
int bid;
int balance;
int received;
int sold;
public:
void getdata()
{
cout<<"\nEnter Book Id: ";
cin>>bid;
cout<<"\nEnter inventory balance at the begining of the month : ";
cin>>balance;
cout<<"\nNumber of copies received during the month : ";
cin>>received;
cout<<"\nNumber of copies sold during the month : ";
cin>>sold;
}
void display()
{
cout<<"\nBook Id: "<<bid;
cout<<"\nUpdated Balance : "<<balance;
}
void update()
{
balance=balance+received-sold;
}
};
void main()
{
book b[5];
clrscr();
for(int i=0;i<5;i++)
{
b[i].getdata();
b[i].update();
b[i].display();
}
getch();
}
Example 9 :
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex() { }
complex(float a) {x=y=a;}
complex(float real,float imag)
{x=real;y=imag;}
void show()
{
cout<<x<<"+j"<<y;
}
void sum(complex,complex);
};
A2.print_data();
}
Example 11 :
#include <iostream.h>
#include<conio.h>
class real
{
int integer_part;
int fractional_part;
public :
real(){}
real(int a)
{
integer_part=a;
fractional_part=a;
}
real(int a, int b)
{
integer_part=a;
fractional_part=b;
}
void sum(real,real);
void print_data();
};
void real::sum(real r1,real r2)
{
integer_part=r1.integer_part+r2.integer_part;
fractional_part=r1.fractional_part+r2.fractional_part;
}
void real::print_data()
{
cout << "Number : " << integer_part << ".";
cout << fractional_part << "\n";
}
void main(void)
{
clrscr();
real r1;
real r2(12);
real r3(121,34);
r2.print_data();
r3.print_data();
r1.sum(r1,r2);
r1.print_data();
getch();
}
Example 12 : Program : 'const' Objects
#include <iostream.h>
#include<conio.h>
class sample
{
int a, b;
public:
sample(int, int);
sample() { }
void display() const; // 'const' member function prototype
~sample() { }
};
sample::sample(int x, int y)
{
a = x; b = y;
}
void sample::display() const // 'const' member function defined
{
cout << "a = " << a << "\t";
cout << "b = " << b << "\n";
}
void main(void)
{
clrscr();
const sample A(10,20);
// 'const' Object declaration
A.display();
// 'const' object invoking 'const' member
function
getch();
}
public :
void set(int);
void print_number( ) const; // 'const' member function prototype
};
void sample::set(int n)
{
num = n;
}
void sample::print_number( ) const // 'const' member function definition
{
cout << "Number = " << num << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
clrscr();
sample a;
a.set(10);
a.print_number( );
getch( );
}
Example 14 :
#include<iostream.h>
#include<conio.h>
class Sample
{
int a;
float b;
public:
Sample(); //constructor
Sample(Sample &ptr);
void display();
};// end of class
Sample::Sample()
{
a=10;
b=20.75;
}
// copy constructor
Sample::Sample(Sample &ptr)
{
a=ptr.a;
b=ptr.b;
}
void Sample::display()
{
cout<<"\na : "<<a<<endl;
cout<<"\nb : "<<b<<endl;
}
void main(void)
{
clrscr();
Sample s1;
s1.display();
Sample s2(s1);
s2.display();
getch();
}//end of main
{
clrscr();
data d(10, 2.5);
cout << "The result returned by the type operator : ";
cout << d.operator float() << "\n";
getch();
}
// Destructor declaration
// Destructor definition
// empty string
}
string::string(char *str)
{
length = strlen(str);
s = new char[length + 1];
strcpy(s,str);
}
void string::concat(string& instring)
{
length = length + instring.length;
strcat(s,instring.s);
}
void string::print_string()
{
cout.width(12);
cout << s;
cout.width(11);
cout << "Length = " << length << "\n";
}
void main(void)
{
clrscr();
string s1("Good "),s2("Morning"),s3;
s3.concat(s1);
s3.concat(s2);
s1.print_string();
s2.print_string();
s3.print_string();
getch();
}
void date::print_time(time t)
{
int h = t.seconds / 3600,
m = (t.seconds % 3600) / 60;
cout << "Time : " << h << ":" << m << "\n";
}
void main(void)
{
clrscr();
date todays_date;
time current_time;
todays_date.set(16,2,2002);
current_time.set(12000);
todays_date.print_date();
todays_date.print_time(current_time);
getch();
}
current_time.set(12000);
print_time(current_time);
getch();
}
{
clrscr();
date todays_date;
time current_time;
todays_date.set(16,2,2002);
current_time.set(12000);
print_datetime(todays_date, current_time);
getch();
}
Example 23 :
#include <iostream.h>
#include <conio.h>
class DB;
class DM
{
int meters;
float centimeters;
public:
void getdata()
{
cout<<"\nEnter Meter : ";
cin>>meters;
cout<<"\nEnter Centimeter : ";
cin>>centimeters;
}
friend void add(DM dm, DB db, int ch);
};
class DB
{
int feet;
float inches;
public:
void getdata()
{
cout<<"\nEnter Feet : ";
cin>>feet;
cout<<"\nEnter Inches : ";
cin>>inches;
}
friend void add(DM dm, DB db, int ch);
};
add(dm,db,ch);
cout<<"\nDo u want to continue?(1/0):";
cin>>ans;
}
while(ans==1);
getch();
}
#include<conio.h>
/*----------------------Class definition-------------------------*/
class sample
{
int a;
public :
// inline member functions
void setdata(int x)
{
a = x;
}
void print_square() {cout << "sqr(a) = " << a*a << "\n";}
void print_cube();
};
// inline member function
inline void sample::print_cube()
{
cout << "cube(a) = " << a*a*a << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
clrscr();
sample num;
num.setdata(10);
num.print_square();
num.print_cube();
getch();
}
Example 29 :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
const int true=1;
const int false=0;
class matrix
{
int maxrow;
int maxcol;
int m[10][10];
public:
matrix()
{
maxrow=0;maxcol=0; }
matrix(int row,int col)
{
maxrow=row;
maxcol=col;
}
void read();
void show();
void add(matrix a,matrix b);
void sub(matrix a,matrix b);
void mul(matrix a,matrix b);
int eq1(matrix b);
};
void matrix :: add(matrix a, matrix b)
{
int i,j;
maxrow=a.maxrow;
maxcol=a.maxcol;
if(a.maxrow!=b.maxrow ||a.maxcol!=b.maxcol)
{
cout<<"\nInvalid order of matrix";
exit(0);
}
for(i=0;i<maxrow;i++)
{
for(j=0;j<maxcol;j++)
m[i][j]=a.m[i][j]+b.m[i][j];
}
}
void matrix :: sub(matrix a, matrix b)
{
int i,j;
maxrow=a.maxrow;
maxcol=a.maxcol;
if(a.maxrow!=b.maxrow ||a.maxcol!=b.maxcol)
{
cout<<"\nInvalid order of matrix";
exit(0);
}
for(i=0;i<maxrow;i++)
{
for(j=0;j<maxcol;j++)
m[i][j]=a.m[i][j]-b.m[i][j];
}
}
void matrix :: mul(matrix a, matrix b)
{
int i,j,k;
maxrow=a.maxrow;
maxcol=a.maxcol;
if(a.maxcol!=b.maxrow)
{
cout<<"\nInvalid order of matrix";
exit(0);
}
for(i=0;i<maxrow;i++)
{
for(j=0;j<maxcol;j++)
{
m[i][j] =0;
for(k=0;k<maxcol;k++)
m[i][j]=m[i][j]+a.m[i][k]*b.m[k][j];
}
}
}
int matrix::eq1(matrix b)
{
int i,j;
for(i=0;i<maxrow;i++)
for(j=0;j<maxcol;j++)
if(m[i][j]!=b.m[i][j])
return 0;
return 1;
}
void matrix::read()
{
int i,j;
for(i=0;i<maxrow;i++)
for(j=0;j<maxcol;j++)
{
cout<<"matrix["<<i<<","<<j<<"]=";
cin>>m[i][j];
}
}
void matrix ::show()
{
int i,j;
for(i=0;i<maxrow;i++)
{
cout<<"\n";
for(j=0;j<maxcol;j++)
cout<<m[i][j]<<" ";
}
}
void main()
{
int m,n,p,q;
clrscr();
cout<<"\nEnter matrix A :";
cout<<"\nEnter rows : ";
cin>>m;
cout<<"\nEnter cols : ";
cin>>n;
matrix a(m,n);
a.read();
cout<<"\nEnter matrix B : ";
cout<<"\nEnter rows : ";
cin>>p;
cout<<"\nEnter cols : ";
cin>>q;
matrix b(p,q);
b.read();
cout<<"\nmatrix A is : ";
a.show();
cout<<"\nmatrix B is : ";
b.show();
matrix c(m,n);
c.add(a,b);
cout<<"\nC=A+B : ";
c.show();
matrix d(m,n);
d.sub(a,b);
cout<<"\nD=A-B : ";
d.show();
matrix e(m,q);
e.mul(a,b);
cout<<"\nE=A*B : ";
e.show();
if(a.eq1(b))
cout<<"\nA = B ?-yes";
else
cout<<"\nA = B ?-no";
getch();
}
Example 30 :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class twodim;
class onedim
{
int array[100];
int row, col;
public:
void getdata();
void dispcolwise();
void disprowwise();
friend void matmul(onedim, onedim,twodim);
};
void onedim::getdata()
{
cout<<"\nEnter no. of rows : ";
cin>>row;
cout<<"\nEnter no. of columns : ";
cin>>col;
cout<<"\nEnter elements : ";
for(int i=0;i<row*col;i++)
cin>>array[i];
}
void onedim::disprowwise()
{
int i,j,in=0;
cout<<"\nMatrix B : ";
for(i=0;i<row;i++)
{
cout<<"\n";
for(j=0;j<col;j++)
cout<<array[in++]<<" ";
}
}
void onedim ::dispcolwise()
{
int i,j,in=0;
cout<<"\nMatrix A : ";
for(i=0;i<row;i++)
{
in=i;
cout<<"\n";
for(j=0;j<col;j++)
{
cout<<array[in]<<" ";
in=in+2;
}
}
}
class twodim
{
int prod[10][10];
int row,col;
public:
friend void matmul(onedim, onedim,twodim);
};
void matmul(onedim a, onedim b, twodim c)
{
int i,j,k,l,m;
if(a.col != b.row)
exit(0);
else
{
c.row = a.row;
c.col = b.col;
}
m=0;
for(i=0;i<c.row;i++)
{
k=0;
for(j=0;j<c.col;j++)
{
c.prod[i][j] =0;
int i1=k,i2=m;
for(l=0;l<a.row;l++)
c.prod[i][j]=c. prod[i][j]+a.array[i1++]*b.array[i2++];
k=k+2;
}
m=m+2;
}
cout<<"\n Product A*B: \n";
for(i=0;i<c.row;i++)
{
cout<<"\n";
for(j=0;j<c.col;j++)
cout<<c.prod[i][j]<<" ";
}
}
void main()
{
onedim a,b;
twodim c;
clrscr();
cout<<"\nEnter elements column wise : ";
a.getdata();
a.dispcolwise();
cout<<"\nEnter elements row wise : ";
b.getdata();
b.disprowwise();
matmul(a,b,c);
getch();
}
cin>>m>>n;
}
void set :: display (void)
{
cout<<" Largest value = "
<<largest( ) <<"\n";
}
void main( )
{
set A;
A.input( );
A.display( );
getch( );
}
int temp = 0;
temp=numerator;
numerator=(*n1).numerator;
(*n1).numerator=temp;
temp=denominator;
denominator=(*n1).denominator;
(*n1).denominator=temp;
}
void main(void)
{
Rational a, b;
clrscr();
a.set(2,5);
b.set(3,7);
a.exchange(&b);
cout << "a = "; a.print();
cout << "b = "; b.print();
getch();
}
Example 34 :
#include<iostream.h>
#include<conio.h>
class book
{
int bid;
int balance;
int received;
int sold;
public:
void getdata()
{
cout<<"\nEnter Book Id: ";
cin>>bid;
cout<<"\nEnter inventory balance at the begining of the month : ";
cin>>balance;
cout<<"\nNumber of copies received during the month : ";
cin>>received;
cout<<"\nNumber of copies sold during the month : ";
cin>>sold;
}
void display()
{
cout<<"\nBook Id: "<<bid;
cout<<"\nUpdated Balance : "<<balance;
}
void update()
{
balance=balance+received-sold;
}
};
void main()
{
book b[5];
clrscr();
for(int i=0;i<5;i++)
{
b[i].getdata();
b[i].update();
b[i].display();
}
getch();
}
Example 35 :
#include<iostream.h>
#include<conio.h>
class distance
{
int feet;
float inches;
public:
distance()
{ feet=0; inches=0.0;}
distance(int ft,float in)
{feet=ft;inches=in;}
void getdist()
{
cout<<"\nEnter feet:";
cin>>feet;
Example 36 :
#include<iostream.h>
#include<conio.h>
class Float
{
float num;
public:
Float(){ }
Float(float n)
{
num=n;
}
void display()
{
cout<<num<<endl;
}
float operator +(Float c)
{
return(num+c.num);
}
float operator *(Float c)
{
return(num*c.num);
}
float operator -(Float c)
{
return(num-c.num);
}
float operator /(Float c)
{
return(num/c.num);
}
};
void main()
{
Float f1(3.14);
Float f2(2.30);
Float f3;
clrscr();
f3=f1+f2;
f3.display();
f3=f1-f2;
f3.display();
f3=f1*f2;
f3.display();
f3=f1/f2;
f3.display();
getch();
}
Example 37 :
#include<iostream.h>
#include<conio.h>
class pow
{
int no;
public:
pow()
{}
void getno();
void dispno();
pow operator^(pow);
};
void pow::getno()
{
cout<<"\nEnter number : ";
cin>>no;
}
void pow:: dispno()
{
cout<<no;
}
pow pow::operator^(pow p1)
{
pow r;
int i,sum=1;
for(i=1;i<=p1.no;i++)
sum*=no;
r.no=sum;
return(r);
}
void main()
{
pow p1,p2,p3;
clrscr();
p1.getno();
p2.getno();
p3=p1^p2;
cout<<"\nResult : ";
p3.dispno();
getch();
}
Example 38 :
# include<iostream.h>
# include<conio.h>
class NUM
{
int a;
public:
void getdata()
{
cout<<"Enter value : " ;
cin>>a;
}
NUM operator +(NUM n)
{
NUM temp;
temp.a=a+n.a;
return (temp);
}
NUM operator -(NUM n)
{
NUM temp;
temp.a=a-n.a;
return (temp);
}
NUM operator *(NUM n)
{
NUM temp;
temp.a=a*n.a;
return (temp);
}
NUM operator /(NUM n)
{
NUM temp;
temp.a=a/n.a;
return (temp);
}
void operator ++()
{
a++;
}
void operator -()
{
a=-a;
}
void disp()
{
cout<<a;
}
};
void main()
{
NUM n1,n2,n3;
clrscr();
n1.getdata();
n2.getdata();
n3=n1+n2;
cout<<"\nn1+n2 : ";
n3.disp();
n3=n1-n2;
cout<<"\nn1-n2 : ";
n3.disp();
n3=n1*n2;
cout<<"\nn1*n2 : ";
n3.disp();
n3=n1/n2;
cout<<"\nn1/n2 : ";
n3.disp();
++n1;
cout<<"\n++n1 : ";
n1.disp();
-n2;
cout<<"\n-n2 : ";
n2.disp();
getch();
}
Example 39 :
#include <iostream.h>
#include<string.h>
#include<conio.h>
class string
{
private :
char* str;
public :
string(char* s)
{
int length = strlen(s);
str = new char[length +1 ];
strcpy(str,s);
}
~string()
{
delete str;
}
void display()
{
cout << str;
}
};
void main()
{
string s1 = "who knows nothing doubts nothing.";
clrscr();
cout <<endl<<"s1 = " ;
s1.display();
getch();
}
strcpy(itemname,s);
cost = c;
}
void sample::print_data()
{
cout << "Item : " << itemname << "\n";
cout << "Cost : " << cost << "\n";
}
void main(void)
{
sample A1("Pencil", 5);
sample A2 = sample("Notebook", 20);
clrscr();
A1.print_data();
A2.print_data();
getch();
}
Example 41 :
#include<iostream.h>
#include<math.h>
#include<conio.h>
class polar
{
private:
double radius;
double angle;
double getx()
{
return radius*cos(angle);
}
double gety()
{
return radius*sin(angle);
}
public:
polar()
{
radius=0.0;
angle=0.0;
}
polar(float r,float a)
{
radius=r;
angle=a;
}
void display()
{
cout<<"("<<radius<<","<<angle<<")";
}
polar operator +(polar p2)
{
double x=getx()+p2.getx();
double y=gety()+p2.gety();
double r=sqrt(x*x+y*y);
double a=atan(y/x);
return polar(r,a);
}
};
void main()
{
polar p1(2.66,1.59),p3;
polar p2(3.16, 2.4);
clrscr();
p3=p1+p2;
p3.display();
getch();
}
string();
string(char*);
// Overloaded Relational operator '=='
int operator == (string);
// Overloded '+' operator
friend string operator + (string, string);
void show();
};
string::string()
{
length = 0;
s = new char[length + 1];
s[0] = '\0';
// empty string
}
string::string(char *str)
{
length = strlen(str);
s = new char[length + 1];
strcpy(s,str);
}
// Overloaded '==' relational operator as a member function
int string::operator == (string x)
{
int flag = 0;
if (length == x.length)
{
flag = 1;
for (int i=0; i<length; i++)
{
if (s[i] != x.s[i])
{
flag = 0;
break;
}
}
}
return flag;
}
void string::show()
{
cout << s << "\t(Length = " << length << ")\n";
}
/*----------------------Definition ends here---------------------*/
// Overloded '+' (concatenation) operator as a friend function
b = y;
}
void sample::print()
{
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
}
/*----------------------Definition ends here---------------------*/
sample operator - (sample x) // friend function definition
{
sample temp;
temp.a = -x.a;
temp.b = -x.b;
return temp;
}
void main(void)
{
sample A(5,10), B;
clrscr();
cout << "Result after first application of unary minus : \n";
B = -A;
// invoking operator function
B.print();
cout << "Result after second application of unary minus : \n";
B = operator - (B); // invoking operator function directly
B.print();
getch();
}
};
void Rational::set(int a, int b)
{
numerator = a;
denominator = b;
}
void Rational::print()
{
cout << numerator << " / " << denominator;
cout << "\n";
}
Rational Rational::sum(Rational n)
{
Rational Tobject;
int temp = 0;
temp += numerator * n.get_denominator();
temp += denominator * n.get_numerator();
Tobject.numerator = temp;
Tobject.denominator = denominator * n.get_denominator();
return Tobject;
}
void main(void)
{
Rational a,b,c;
clrscr();
a.set(2,5);
b.set(3,7);
c.set(0,0);
c = a.sum(b);
cout << "a = "; a.print();
cout << "b = "; b.print();
cout << "c = "; c.print();
getch();
}
Example 45 :
#include <iostream.h>
#include <conio.h>
class Runner
{
char name[20];
int time;
public:
void getdata()
{
cout<<"\nEnter name :";
cin>>name;
cout<<"\nEnter time in minutes : ";
cin>>time;
}
friend void result(Runner ,Runner, Runner);
};
void result(Runner r1,Runner r2 , Runner r3)
{
if(r1.time<r2.time)
if(r1.time<r3.time)
{
cout<<"\n First : "<<r1.name;
if(r2.time<r3.time)
{
cout<<"\n Second : "<<r2.name;
cout<<"\n Third : "<<r3.name;
}
else
{
cout<<"\n Second : "<<r3.name;
cout<<"\n Third : "<<r2.name;
}
}
else
{
cout<<"\n First : "<<r3.name;
cout<<"\n Second : "<<r1.name;
cout<<"\n Third : "<<r2.name;
}
else
if(r2.time<r3.time)
{
cout<<"\n First : "<<r2.name;
if(r1.time<r3.time)
{
cout<<"\n Second : "<<r1.name;
cout<<"\n Third : "<<r3.name;
}
else
{
// initializes count to 0
cin>>itemprice[count];
count++;
}
void item :: displaysum(void)
//display total value of all items
{
float sum = 0;
for(int i = 0; i<count; i++)
sum=sum + itemprice[i];
cout<<"\n Total value :"<<sum<<"\n";
}
void item :: remove(void)
//deleting a specified item
{
int a;
cout<<" Enter item code : ";
cin>>a;
for(int i = 0; i<count; i++)
if (itemcode[i] == a)
itemprice[i] = 0;
}
void item :: displayitems(void)
//displaying items
{
cout<<"\n Code
Price \n ";
for(int i = 0; i<count; i++)
{
cout<<"\n"<<itemcode[i];
cout<<"
"<<itemprice[i];
}
cout<<"\n";
}
int main( )
{
item I;
I.cnt( );
int ch;
do
{
cout<<"\n Option are. \n";
cout<<"\n1. Add an item ";
cout<<"\n2. Display total value ";
cout<<"\n3. Delete an item";
cout<<"\n4. Display all items ";
cout<<"\n5. Exit";
cout<<"\n\n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : I.getitem( ); break;
case 2 : I.displaysum( ); break;
case 3 : I.remove( ); break;
case 4 : I.displayitems( ); break;
case 5 : exit(0);
default : cout<<"\n Error in input; try again \n";
}
} while(ch !=5);
}
Example 47 :
# include <iostream.h>
#include<conio.h>
class Sample
{
static int count;
int number;
public :
void getdata (int a)
{
number = a;
count ++;
}
void getcount (void)
{
cout << "Count : "<< count <<endl;
}
};
int Sample :: count;
void main ( )
{
Sample a, b, c;// count is initialized to zero display count
clrscr();
a.getcount ( );
b.getcount ( );
c.getcount ( );
a.getdata (10); // getting data into object a
b.getdata (20); // getting data into object b
c.getdata (30); // getting data into object c
cout << "After reading data" << "\n";
Example 48 :
#include <iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
// static member variable
public :
void setcode (void)
{
code = ++count;
}
void showcode (void)
{
cout << "Object number : " << code << "\n";
}
static void showcount(void)
// static number function
{
cout << "Count : "<<count<<"\n";
}
};
int test :: count;
int main()
{
test t1, t2;
clrscr();
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode ( );
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
sample A(10,20);
clrscr();
cout << "Before the application of operator '-'(unary minus) :\n";
A.print();
-A;
// operator function gets invoked
cout << "After the application of operator '-'(unary minus) :\n";
A.print();
A.operator -(); // another way of invoking - as a member function
cout << "After the second application of operator '-'(unary minus) :\n";
A.print();
getch();
}
Example 51 :
#include<iostream.h>
#include<conio.h>
class Floatvector
{
float v[100];
int n;
public:
void create();
void display();
void modify();
void multiply();
};
void Floatvector::create()
{
cout<<"\nEnter no. of elements : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>v[i];
}
void Floatvector:: display()
{
cout<<"\nVector : ";
for(int i=0;i<n;i++)
cout<<v[i]<<",";
}
void Floatvector::modify()
{
int pos;
float num;