Sahabpreet C++

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 36

CLASS IMPLEMENTATION

#include<iostream.h>
#include<conio.h>
class item
{
int number; // private by deafult
float cost; // private by deafult
public:
void getdata (int a, float b); // prototype declaration,
// to be defined
// function defined inside class
void putdata (void)
{
cout << "number:" << number << "\n";
cout << "cost:" << cost <<"\n";
}
};
//....member function definition..............
void item::getdata (int a, float b) // use membership label
{
number = a; // private variables
cost = b; // directly used
}
//........main program.........
void main ()
{ // create object x
item x;
clrscr();
cout << "\nobject x "<< "\n";
// call member function
// call member function
item y; // create another object
cout << "\nobject y" <<"\n";
y.getdata (200, 175.50);
y.putdata ();
getch();
}

output:
object x
number : 100
cost : 299.95
object y
number : 200
cost : 175.5
NESTING OF MEMBER FUNCTIONS
#include <iostream.h>
#include<conio.h>
#include<string.h>

class binary
{
string s;
public:
void read (void)
{
cout <<"enter a binary number:";
cin >>s;
}
viod chk_bin (void)
{
for (int i=0; i<s, length (); i++)
{
if (s, at (i) ! = '0' && s.at (i) ! ='1')
{
cout<<"\nincorrect binary number format.... the program will
quit";
getch();
exit(0);
}
}
}
viod ones (void)
{
chk_bin ; //calling nmember function
for (int i=0; i<s. length () ;i++)
{
if (s.at (i) =='0')
s.at (i) = '1';
else
s.at (i)='0'
}
}
void displayones (void)
{
ones (); //calling member function
cout<<"\nthe 2's compliment of the above binary number is:
",,s;
}
};
itn main ()
{
binary b;
b.red ();
b.displayones();
getch ();
return 0;
}

output 1 :
enater a binary number ; 110101
the 1’s compliment of the above binary number is : 001010
output .”
enter a binary number : 1101210
incorrect binary number format….the program will quit
PROCESSING SHOPPING LIST
#include <iostream.h>
using namesapce std;
const m=50;
class items
{
int itemcode [m];
float itemprice [m];
int count;
public:
void cnt (void) {count = 0;) //initializes count to 0
void getitem (void);
void displaysum (void);
void remove (void);
void displayitems (void);
};
//========================
void items :: getitme (void) //assign vlaues to data
// members of itme
{
cout<<"enter item code:";
cin>> itemcode [count];
cout <<"enter item cost:";
cin >> itemprice [count];
count++
}
void items :: dispolaysum (void) //diplay total values of
all items
{
float sum = 0;
for (int i=0; i<count; i++)
sum= sum + itemprice [};
cout << "\ntotal value :" << sum << "\n";
}
void items :: remove (void) //delete a specified item
{
int a;
cout<< "enter item code:";
cin >>a;
for (int i=0; i<count; i++)
if (itemcode[i] ==a)
itemprice b[i] =0;
}
void items :: displayitems (void) //displaying items
{
cout <<>"\ncode price \n";
for (int i=0; i<count; i++)
{
cout <<"\n" <<itemcode[i];
cout <<" " << itemprice [i];
}
cout <<"\n";
}
//==================
int main ()
{
item order;
order.cnt ();
int x;
do // do....while loop
cout <<"\nyou can do the following;"
<<"enter appropriate number \n";
cout <<"\n1 : add an item ";
cout << "\n2 : display total item";
cout <<"\n3 : delete an item";
cout <<"\n4 : display all items";
cout <<"\n5 : quit";
cout <<"\n/nwhat is your option?";
cin >> x;
switch (x)
{
case1: order.getitem(); break;
case2: order.displaysum(); break;
case3: order.remove(); break;
case4: order.displayitems(); break;
case5: break;
default : cout << "error in input; try again\n";
}
} while (x !=5); //do....while ends
return 0;
}

output:
you can do the following : enter appropriate number:
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?1
enter item code: 111
enter item cost : 100
you can do the following; enter appropriate number
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?1
enter item code: 222
enter item cost : 220
you can do the following; enter appropriate number
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?1
enter item code: 333
enter item cost : 300
you can do the following; enter appropriate number
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?2
total value : 600
you can do the following; enter appropriate number
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?3
enter item code : 222
you can do the following; enter appropriate number
1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?4
code price
111 100
222 200
333 300

you can do the following; enter appropriate number


1: add an item
2: display total value
3: delete an item
4: display all items:
5: quit
what is your option?5
STATIC CLASS MEMBER
#include<iostream.h>
#include<conio.h>

class item
{
static int count;
int number;
public:
void getdata (int a)
{
number = a;
count ++;
}
void getcount (void)
{
cout <<”count:”;
cout <<count<<”\n”;
}
};
int item :: count;
int main()
{
item a, b, c,; // count is initialized to zero
getcount (); // display count
b.getcount ();
c.getcount ();
a.getdata (100); // getting data into object a
b.getdata (200); // getting data into object b
c.getdata (300); // getting data into object c
cout << “after reading data” <<”\n”;
a.getcount (); //display count
b.getcount ();
c.getcoutn();
return 0;
}

output:
count : 0
count : 0
count : 0
after reading data
count : 3
count : 3
count : 3
STATIC MEMBER FUNCTION
#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 9void) // static member function
{
cout <<”count; “<<count <<”\n”;
};
int test :: count;
int main()
{
test t1, t2;
t1.setcode();
t2.setcode ();
test :: showcount (); //accessing static
function
test t3;
t3.setcode();
test :: showcount ();
t1.showcoded ();
t2.showcoded ();
t3.showcoded ();
return 0;
}

output:
count : 2
count : 3
object number : 1
object number : 2
object number : 3
OBJECTS AS ARGUMENTS
#include<iostream.h>
#include<conio.h>
using namespace std
class time
{
int hours;
int minutes;
public:
void gettime (int h, int m)
{ hours = h; minutes =m;)
void puttime (void)
{
cout <<hours <<”hours and”;
cout <<minutes <<”minutes” <<”\n”;
}
void sum(time,time); // declaration with objects as arguments
};
void time :: sum(time t1, time2) // t1,t2 are objects
{
minutes = t1.minutes + t2.miutes;
hours = minutes/60;
minutes = minutes%60;
hours = hours + t1.hours +t2.hours;
}
int main()
{
time t1, t2, t3;
t1.gettime (2,45); // get t1
t2.gettime (3,30); // get t2
t3.sum (t1,t2); // t3=t1+t2
cout <<”t1=”;t1.puttime(); // display t1
cout <<”t2=”;t2.puttime(); // display t2
cout <<”t3=”;t3.puttime(); // display t3
return 0;
}

output:
t1 = 2 hours and 45 minutes
t2 = 3 hours and 30 minutes
t3 = 6 hours and 15 minutes
ARRAYS OF OBJECTS
#include<iostream.h>
#include<conio.h>

class employee
{
char name[30]; //string as class member
float age;
public;
void getdata(void);
void putdata(void);
};
void employee:: getdata (void)
{
cout<<”enter name:”;
cin >> name;
cout <<”enter age:”;
cin >> age;
}
void employee :: putdata (void)
{
cout <<”name:” <<name <<”\n”;
cout <<”age:” <<age <<”\n”;
}
cons tint size=3
int main ()
{
employee manager ([size];
for (int i=0; i<size; i++)
{
cout <<”\ndetails of manager” <<i+1 <<”\n”;
manager[i].getdata();
}
cout <<”\n”;
for (i=ol i<size; i++)
{
cout <<”\nmanager” << i+1<<”\n”;
manager[i].putdata();
}
return 0;
}

output:
interactive input:
details of manger1
enter name: xxx
enter age :45

details of manger2
enter name : yyy
enter age :37

details of manger3
enter name : zzz
enter age :50

program output
manger1
name : xxx
age :45
manger2
name : yyy
age :37

manger3
name : zzz
age :50
FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>

class sample
{
int a;
int b;
public:
void setvalue () {a=25; b= 40;}
friend float mean (samle s);
};
float mean (sample s)
{
return float (s.a.+s.b)/2.0;
}
int main()

sample x; // object x
x.setavlue();
cou<<”mean value=” <<mean (x) <<”\n”;
return 0;
}

output:
mean value = 32.5
USING FRIEND FUNCTION TO ADD DATA OBJECTS OF TWO DIFFERENT
CLASSES
#include<iostream.h>
#include<conio.h>

class abc; //forward declaration


//-----------------------------------------------------//
class xyz
{
int data;
public:
void setvalue (int value)
{
data=value;
}
friend void add (xyz, abc); //friend function declaration
};
//------------------------------------------------------//
class abc
{
int data;
public:
void setvalue (int value)
{
data=value;
}
friend void add (xyz, abc); // friend function declaration
};
//--------------------------------------------------//
void add (xyz obj1, abc obj2) // friend function definition
{
cout<<”sum of data values of xyz and abc objects using friend
function = “<<obj1.data+obj2.data;
}
//--------------------------------------------------//
int main()
{
xyz x;
abc a;
x.setvalue(5);
a.setvalue(50);
add(x,a); //calling friend function
return 0;
}

output
sum of data values of xyz and abc objects using friend function =
55
DEREFERENCING OPERATORS
#include<iostream.h>
#include<conio.h>

class m
{
int x;
int y;
public:
void set_xy(int a, int b);
{
x = a;
y = b;
}
friend int sum (m m);
};
int sum (m m)
{
int m ::*px=&m::x’
int m ::*py=&my;
m*pm=&m;
int s=m*px+pm>>*py;
return s;
}
int main()
{
m n;
void (m::*pf) (int, int) = &m:: set_xy;
(n.*pf) (10,20);
cout<<”sum =” <<sum (n) <<”\n”;
m*op +&n;
(op->*pf(30,40);
cout <<”sum = “<<sum(n) <<”\n”;
return 0;
}

output:
sum = 30
sum = 70
EXAMPLE OF PARAMETERIZED CONSTRUCTOR
//this program defines a class called point that stores the x and
y coordinates of a point. the class uses parameterized constructor
for initializing the class objects.

#include<iostream.h>
#include<conio.h>

class point
{
int x, y;
public:
point (in a, int b) //inline parameterized cobstructor
definition
{
x=a;
y=b;
}
void display()
{
cout<<”(“<<x<<”,””<<y<<”)(\n”;
}
}
;
int main()
{
point p1(1,1); //invokes parameterized constructor
point p2(5,10);
cout<<”point p1=”;
p1.display();
cout<<”point p2=”;
p2.display();
return 0;
}

output:
point p1 = (1,1)
point p2 = (5,10)
OVERLOADED CONSTRUCTORS
#include<iostream.h>
#include<conio.h>

class complex
{
float x,y;
public:
complex () { } //constructor non arg
complex (float a) {x=y=a;} //constructor one arg
complex (float real, float imag) //constructor two args
{x=real; y= imag;}
friend complex sum(complex; complex);
friend void show (compex);
{;
complex sum (complex c1, complex c2) //friend
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.3y;
return (c3);
}
void show (complex c) //friend
{
cout<<c.x<<”+”+j”<<c.y<<”\n”;
}
int main()
{
complex a(2.7, 3.5); //define & initialize
complex b(1.6); //define & initialize
complex c; //define
c= sum (a,b); //sum () is a friend
cout<<”a=”; show (a); //show () is also friend
cout<<”b=”; show (b);
cout<<”c=”;; show (c);
//another way to give initial values (second method)
complex p,q,r; //define p, q and r
p = complex (2.5, 3.9); //initialize p
q = complex (1.6, 2.5); //initialize q
r = sum (p,q);

cout<<”\n”;
cout<<”p=”; show (p);
cout<<”q=”; show (q);
cout<<”r=”; show ®;
return 0;
}

output:
a = 2.7 + j3.5
b = 1.6 + j1.6
c = 4.3 + j5.1

p = 2.5 + j3.9
q = 1.6 + j2.5
r = 4.1+ j6.4
COPY CONSTRUCTOR
#include<iostream.h>
#include<conio.h>

class code
{
int id;
public:
code () { } //constructor
code (int a) (id =a;} //constructor again
code (code & x) //copy constructor
{
id = x.id; //copy in the value
}
void display (void)
{
cout<<id;
}
};
int main()
{
code a(100;) //object a is created and
initialized
code b(a); //copy constructor called
code c=a; //copy constructor called again
code d; // d is created, not initialized
d=a; //copy constructor not called
cout<<”\n id of a: “; a.display();
cout<<”\n id of b:” ; b.display();
cout<<”\n id of c:” ; b.display();
cout<<”\n id of d:” ; b.display();
return 0;
}

output
id of a; 100
id of b: 100
id of c: 100
id of d: 100
CONSTRUCTORS WITH NEW
#include<iostream.h>
#include<conio.h>

class string
{
char *name;
int length;
public:
string(0 //constructor-1
{
length =0;
name = new char [length+1];
}
string (char*s) //constrictor-2
{
length =strlen (s);
name = new char [length+1]; //one additional
//character for\0
strcpy (name, s);
}
void display9void)
{cout<<name<<”\n”;}
void join (string &a, string &b(;
};
void string :: joing (string &a, strin &b)
{
length = a.length+b.length;
delete name;
name= new char [length+1]; //dynamic allocation
strcpy (name, a.name);
strcat (name, b.name);
};
int main()
{
char *first=”joseph”;
string name1(first), name2 (“louis”), name3 (“largrange”), s1,s2;
s1.join(name1, name2);
s2.join(s1, name3);
name1.display();
name2.display();
s1.display();
s2.display()
return 0;
}

output:
joseph
louis
lagrange
joseph louis
joseph louis lagrange
IMPLEMENTATION OF DESTRUCTORS
#include<iostream.h>
#include<conio.h>

int count=0;
class test
{
public:
test ()
{
count++;
cout<<”\n\nconstructor msg: object number”<<count<<”created..”;
}
~test()
{
cout<<”\n\ndestructor msg: object number “<<count<<”destroyed..”;
count--;
}
};
int main()
{
cout<<”inside the main block..”;
cout<<”\n\ncreating first object t1…”;
test t1;
{ //block1
cout<<”\n\ninside block 1..”;
cout<<”\n\ncreating two more objects t2 and t3..”;
test t2,t3;
cout<<”\n\nleaving block 1..”;
}
cout<<”\n\nback inside the main block..”;
return 0;
}

output
inside the main block..
creating first object t1..
constructor msg: object number 1 created..
inside block 1…
creating two more objects t2 and t3…
constructor msg: object number 2 created…
constructor msg: object number 3 created…
leaving block 1..
destructor msg: object number 3 destroyed.
destructor msg: object number 2 destroyed.
back inside the main block..
destructor msg: object number 1 destroyed..
OVERLOADING UNARY MINUS
#include<iostream.h>
#include<conio.h>

class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display (void);
void operator- (); //overload unary minus
};
void space :: getdata (int a, int b, int c)
{
x =a;
y =b;
z=c;
}
void space :: display (void)
{
cout<<”x=”<<x<<” ”;
cout<<”y=”<<y<<” “;
cout<<”z=”<<z<<”\n”;
}
void space :: operator- ()
{
x = -x;
y = -y;
z = -z;
}
int main()
{
space s;
s.getdata(10, -20, 30);
cout<<”s:”;
s.display();-s; //activates operator-() function
return 0;
}

output:
s : x= 10y = -20 z= 30
-s : x= -10y = 20 z= -30
OVERLOADING + OPERATOR
#include<iostream.h>
#include<conio.h>

class complex
{
float x; //real part
float y; // imaginary part
public:
complex() { } // constructor 1
complex (float real, float imag) // constructor 2
void display (void);
};
complex complex :: operator+(complex c)
{
complex temp; // temporary
temp.x = x+c.x; // these are
temp.y = y+c,y; // float additions
return (temp);
}
void complex :: display(void)
{
cout<<x<<”+j”<<y<<”\n”;
}
int main()
{
complex c1, c2, c3
c1 = complex (2.5, 3.5); // invokes constructor 1
c2 = complex (1.6, 2.7); // invokes constructor 2
c3 = c1+c2;
cout<<”c1=”; c1.display();
cout<<”c2=”; c2.display();
cout<<”c3=”; c3.display();
return 0;
}

output
c1 = 2.5 + j3.5
c2 = 1.6 + j2.7
c3 = 4.1 + j6.2
SINGLE INHERITANCE : PUBLIC
#include<iostream.h>
#include<conio.h>
class b
{
int a ; //private; not inheritable
public:
int b; //public; ready for inheritance
void set_ab();
int get_a(void);
void show_a(void);
};
class d: public b //public derivation
{
int c;
public:
void mul(void);
void display (void);
};
//-------------------------------------------------
void b :: set_ab(void)
{
a=5; b=10;
{
int b :: get_a()
{
return a;
}
void b:: show_a()
{
cout <<”a=”<<a”\n”;
}
void d :: mul()
{
c= b*get_a();
}
void d :: display()
{
cout<<”a=”<<get_a()<<”\n”;
cout<<”b=”<<b<<”\n”;
cout<<”c=”<<c<<”\n\n”;
}
//---------------------------------------------------
int main()
{
d d;
d.set_ab();
d.mul();
d.show_a();
d.display();
d.b= 20;
d.mul();
d.display();
return 0;
}
output
a = 5
a = 5
b = 10
c = 50
a = 5
b = 20
c = 100
MULTILEVEL INHERITANCE
#include<iostream.h>
#include<conio.h>

class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number (int a)
{
roll_number =a;
}
void student :: put_number ()
{
cout<<”roll number:”<<roll_number<<”\n”;
}
class test : public student // first level derivation
{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks (float x, float y)
{
sub1 =x;
sub2 =y;
}
void test :: put_marks()
{
cout<<”marks in sub1=”<<sub1<<”\n”;
cout<<”marks in sub2=”<<sub2<<”\n”;
}
class result : public test //second level derivation
{
float total; // private by default
public:
void display (void);
};
void result :: display(void)
{
total = sub+sub2;
put_number();
put_marks();
cout<<”Total=”<<total<<”\n”
}
Int main()
{
Result student1; /student created
Student1.get_number(111);
Student1.get_marks(75.0, 59.5);
Student1.display();
Return 0;
}

Output
Roll Number: 111
Marks in SUB1: 75
Marks in SUB2: 59.5
Total = 134.5
MULTIPLE INHERITANCE
#include<iostream.h>
#include<conio.h>

class M
{
protected:
int m;
public:
void get_m (int);
};
class N
{
protected:
int n;
public:
void get_n (int);
};
class P: public M, public n
{
public:
void display (void);
};
m = x;
}
void N :: get_n(int y)
{
n =y;
void P:: display (void)
{
cout<<”m=”<<m<<”\n”;
cout<<”n=”<<n<>”\n”;
cout<<”m*n=”<<m*n<<”\n”;
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}

Output
m = 10
n = 20
m*n = 200
HYBRID INHERITANCE
#include<iostream.h>
#include<conio.h>

class student
{
protected:
int roll_number;
public:
void get_number (int a)
{
roll_number = a;
}
void put_number (void)
{
cout<<”Roll no:”<<roll_number<<”\n”;
}
};
class test : public student
{
protected:
float part1, part2;
public:
void get_marks (float x, float y)
{
part1 =x; part2 =y;
}
void pu_marks(void)
{
cout<<”Marks obtained: “<<”\n”
<<”part1 = “<<part1 <<”\n”
<<”part2 = “<<part2<<”\nN;
}
};
class sports
{
protected;
float score;
public:
void get_score (float s)
{
score = s;
}
void put_score(void)
{
cout<<”Sports wt:”<<score<”\n”;
}
};
class result : public test, public sports
{
float total;
public:
void display (void);
};
void result :: display (void)
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout<<”total Score:” <<total<<”\n”;
}
int main()
{
result student_1;
student_1.get_number (1234);
student_1.get_makrs(27.5, 33.0);
student_1.get_score)6.0);
student_1.display();
return 0;
}

Output
Roll No: 1234
Marks Obtained:
Part1 = 27.5
Part2 = 33
Sports wt: 6
Total Score: 66.5
VIRTUAL BASE CLASS
#include<iostream.h>
#include<conio.h>

class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number =a;
}
void put_number(void)
{
cout<<”Roll no:”<<roll_number<<”\n”;
}
};
class test: virtual public student
{
protected;
float part1, part2;
public:
void get_marks(float x, float y)
{
part1 =x; part2 =y;
}
void put_marks(void)
{
cout <<”marks obtained: “<<”\n”
<<Part1=”<<part1<<”\n”
<<”part2=”<<part2<<”\n”;
}
};
class sports : public virtual student
{
protected:
float socre;
public:
void get_score (vloats)
{
score = s;
}
void put_score (void)
{
cout<<”Sports wt:”<<score<<”\n\n”;
}
};
class result : public test, public sports
{
float total;
public:
void display (void);
};
void result :: display (void)
{
total = part1 + part2 + score;
put_number(0;
put_marks();
put_score();
cout<<”total score:”<<total<<”\n”;
}
int main()
{
result student_1;
student_1.get_number (678);
student_1.get_marks (30.5, 25.5);
student-1.get_score(7.0);
student_1.display();
return 0;
}

Output
Roll No: 678
Mark Obtained:
Part1= 30.5
Part2 = 25.5
Sport wt: 7
Total Score : 63
EXAMPLE OF USING POINTERS
#include<iostream.h>
#include<conio.h>

void main()
{
int a, *ptr1, **ptr2;
clrscr();
ptr1=&a;
ptr2+&ptr1;
cout <<”The address of a : “<<ptr1<<”\n”;
cout <<”The address of ptr1:” <<ptr2;
cout <<”\n\n”;
cout <<”After incrementing the address values: \n\n”;
ptr1+=2;
cout<<”The address of a :”<<ptr1<<”\n”;
ptr2+=2;
cout<<”The address of ptr1:”<<ptr2<<”\n”;
getch();
return 0;
}

Output
The address of a: 0x8fb6fff4
The address of ptr1: 0x8fb6fff2
After incrementing the address values:
The address of a : 0x8fb6fff8
The address of b : 0x8fb6fff6
ARRAYS OF POINTERS
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

void main()
{
int i=0
char *ptr[10]={
“books”
“television”,
“computer”,
“sports”
};
char str [25];
clrscr();
cout<<”\n\n\n\nEnter your favorite leisure pursuit:”;
cin>> str;
for(i=0; i<4; i++)
{
if (strcmp (str, *ptr[i]))
{
cout<<”\n\nYour favorite pursuit”<<”is available here”
<<end1;
break;
}
}
if (i==4)
cout<<”\n\nYour favorite”<<”notavailable here”<<end1;
getch();
}

Output
Enter your favorite leisure pursuit : Books
Your favorite pursuit is available here
POINTERS TO OBJECTS
#include<iostream.h>
#include<conio.h>
class item
{
int code;
float price;
public:
void getdata (int a, float b)
{
code = a;
price = b;
}
void show (void)
{
cout<<”Code:”<<code<<”\n”;
cout<<”Price:”<<price<<”\n”;
}
};
const int size =2;
int main()
{
item *p = new item [size];
item *d = p;
int x, i;
float y;
for (i=0; i<size; i++)
{
cout<<”Input code and price for item”<<i+1;
cin>> x>>y;
p->getdata (x,y);
p++;
}
for (i=0; i<size; i++)
{
cout<<”Item:”<<i+1<<”\n”;
d->show();
d++;
}
return 0;

Output
Input code and price for item1 40 500
Input code and price for item2 50 600
Item: 1
Code: 40
Price: 500
Item: 2
Code : 50
Price : 600
THIS POINTER
#include<iostream.h>
#include<conio.h>
#include<cstring.h>
class person
{
char name [20];
float age;
public:
person (char *s, float a)
{
strcpy (name, s);
age =a;
}
person & person :: greater (person & x)
{
if (x.age >=age)
return x;
else
return *this;
}
void display (void)
{
cout<<”Name:”<<name<<”\n”
<<”Age:”<<age<<”\n”;
}
};
int main()
{
person P1 (“John”, 37.50).
P2 (“Ahmed”, 29.0),
P3 (“Hebber”, 40.25);
person P = P1.greater (P3); //P3.greater(P1)
cout<<”Elder person is: \N”;
P.display();
P = P1.greater(P3); // P2.greater (P1)
cout<<”Elder person is: \n”;
P.display();
return 0;
}

Output
Elder person is:
Name : Hebber
Age : 40.25
Elder Person is
Name John
Age : 37.5
VIRTUAL FUNCTIONS
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void display () {cout <<”\nDisplay base”;}
virtual void show() {cout <<”\nshow base”;)
};
class Derived : Public Base
{
public:
void display () {cout <<”\nDisplay derived”;)
void show() {cout<<”\nShow derived”;)
};
int main()
{
Base B;
Derived D;
Base *bptr;
bptr = &B;
bptr -> display(); // calls base version
bptr-> show(); // calls base version
cout <<”\n\n bptr points to Derived\n”;
bptr = &D;
bptr-> display(); //calls Base version
bptr-> show (); //calls derived version
return 0;
}

Output
bptr points to Base
Display base
Show base
bptr points to derived
Display base
Show derived
RUNTIME POLYMORPHISM
#include<iostream.h>
#include<conio.h>
#include<cstring.h>
class media
{
protected:
char title [50];
float price;
public:
media (char *s, float a)
{
strcpy (titel, s);
price = a;
}
virtual void display() { } //empty virtual function
};
class book: public media
{
int pages;
public:
book (char *s, float a, int p): media 9s,a)
{
pages + p;
}
void display();
};
class tape : public media
{
float time;
public:
tape (char *s, float a, float t); ,eida (s,a)
{
time = t;
}
void display();
};
void book :: display ()
{
cout <<”\nTitel:”<<title;
cout<<”\nPages:”<<pages;
cout<<”\nPrice:”<<price;
}
void tape :: display ()
{
cout<<”\nTitel:”<<title;
cout<<”\nPlay time:”<<time<<”mins”;
cout<<”\nPrice:”<,price;
}
int main()
{
char *title = new char [30];
float price, time;
int pages;
// Book details
cout<<”\n ENTER BOOK DETAILS\N”;
cout<<”Tilte: “; cin >>title;
cout<<”Price:”; cin>>price;
cout<<”Pages:”; cin>> pages;
book book1(title, price, pages);
//Tape details
cout<<’\n ENTER TAPE DETAILS\n”;
cout<<”Title:”:cin>>title;
cout<<”Price; “; cin>> price;
cout<<”Play time (mins): “; cin>> time;
tape tap1(titel, price, time);
media* list [2]
list [0] = &book1;
list[1] = &tape1;
cout<<”\n MEDIA DETAILS”;
cout<<”\n…….. BOOJ……”;
list[0]-> display ();// display book details
cout<<”\n………..TAPE……”;
list[1] -> display(); //display tape details
resukt 0;
}

Output
ENTER BOOK DETAILS
Title: Prpgramming_in_ANSI_C
Price : 88
Pages : 4000
ENTER TAPE DETAILS
Title: Computing_Concepts
Price: : 90
Play time (mins) : 55

MEDIA DETAILS
…….BOOK………
Title: Programming_in_ANSI_C
Pages: 400
Price : 88

……TAPE……
Title: Comput_Concepts
Play time: 55mins
Price : 90
PURE VIRTUAL FUNCTION
#include<iostream.h>
#include<conio.h>
class Balagurusamy //base class
{
public:
virtual void example ()=0; // Denotes pure virtual Function
definition
};
class C:public Balagurusamy //derived class1
{
public:
void example()
{
cout<<”C text Book written by Balagurusamy”;
}
};
class opps: public Balagurusamy //derived class 2
{
public:
void example ()
{
cout<<”C++ text book written by Balagurusamy”;
}
};
void main()
{
Exforsys* arra [2]
C e1;
oops e2;
arra [0]=&e1;
arra [1]=&e2;
arra [0] ->example();
arra[1]-> example ();
}

Output
C text Book written by Balagurusamy
C++ text Book written by Balagurusamy

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy