Object Oriented Programming
Object Oriented Programming
EIGHTH SEMESTER
OBJECT ORIENTED PROGRAMMING
Name: ____________________________________________
Semester: _________________________________________
Batch: ____________________________________________
C
COON
NTTE
ENNT
TSS
• Introduction to arrays.
• Array and pointers.
• Display even values using array.
6 • Display odd values using array.
• Using array with pointer printing numbers in reverse.
• Writes a program of array with pointer to print out the array elements in descending order.
• Using array with pointer to show elements of array in ascending order.
8 • Using array with pointer to input in pointer and than saved in array.
• Implementation Of Inheritance
14 • Multiple Inheritance
• Link List
15
16 • Virtual Function
EXPERIMENT NO – 01
PROGRAM # 1 Printing name and star pattern
#include<iostream.h>
void main()
{
cout<<"\n Abrar\n";
cout<<"\t\n *\n";
cout<<"\t\n ***\n";
cout<<"\t\n *****\n";
cout<<"\t\n ***\n";
cout<<"\t\n *";
}
#include<iostream.h>
void main()
{
cout<<"\tELECTRICAL ENGINEERING DEPARTMENT\n";
cout<<"\n\t7TH SEMESTER EVENING\n";
cout<<"\n\ttSUBMITTED BY Abrar Aziz\n";
cout<<"\n\tREGESTRATION NUMBER 1585\n";
EXPERIMENT NO – 02
PROGRAM # 1 Print name by using for loop
#include<iostream.h>
void main()
{
for(int a=1;a<=5;a++)
cout<<"\nAbrar";
}
cout<<"\n\nfactolial is\t\t"<<fact<<"\n";
}
EXPERIMENT NO – 03
PROGRAM # 1 Conversion in centigrade Fahrenheit and Kelvin
#include<iostream.h>
void main ()
{
float k,c,f;
cout<<"\nEnter temperatuer in centi grade is = \t";
cin>>c;
k=273+c;
cout<<"\nThe tempratuer in Kalwen is = \t"<<k<<"K\n";
f=9/5*c+32;
cout<<"\nThe tempratuer in faran hight is = \t"<<f<<"F\n";
#include <iostream.h>
void main ()
{
float c,f,k;
cout<<"\nTempratuer in Kalven = \t";
cin>>k;
c=k-273;
#include <iostream.h>
void main ()
{
float c,f,k;
cout<<"\nTempratuer in Faran hight = \t";
cin>>f;
c=f-32*5/9;
cout<<"\nTemperatuer in centi grade is = \t "<<c<<"c";
k=273+c;
cout<<"\ntempratuer in Kalven is\t"<<k<<"k\n";
}
#include<iostream.h>
void main()
{
int a[10],b,c=0;
cout<<"ENTER 10 NUMBERS ONE BY ONE\n";
cout<<"PRESS ENTER AFTER EACH ENTRY\n";
for(b=0;b<10;b++)
cin>>a[b];
c=a[0];
for(b=0;b<10;b++)
if(a[b]<c)
c=a[b];
cout<<"\nSMALLEST NUMBER IS "<<c<<"\n";
}
#include<iostream.h>
void main()
{
int a[10],b,c;
c=0;
cout<<"ENTER 10 NUMBERS ONE BY ONE\n";
cout<<"PRESS ENTER AFTER EACH ENTRY\n";
for(b=0;b<10;b++)
cin>>a[b];
c=a[0];
for(b=0;b<10;b++)
if(a[b]>c)
c=a[b];
cout<<"\n\nLARGEST NUMBER IS "<<c<<";
}
PROGRAM # 6 Program takes the input from user and show the sum of all. (Quiz)
#include<iostream.h>
void main ()
{
float a,sum,b,c;
sum=0;
for (int i=0; i<10; i++)
{
cout<<"\nenter number for addition\t";
cin>>a;
if (a<11)
{
sum=sum+a;
#include<iostream.h>
void main()
{
char option;
float a,b,c,i,fact;
cout<<"Assignment # 1\nName\t\t\t Abrar Aziz\nregistration #\t\t1585-ID-BE-
(A)-05\nSemester\t\t 7th (Eve)\n\n\n\n";
cout<<"Enter the button which function you want to perform\n";
cout<< "enter + for addition\n";
cout<< "enter - for Subtraction\n";
cout<< "enter / for Division\n";
cout<< "enter * for multiplication\n";
cout<< "enter ! for factorial\n";
cin>>option;
switch (option)
{
case'+':
{
cout<<"enter 1st number\t";
cin>>a;
cout<<"enter 2nd number\t";
cin>>b;
c=a+b;
cout<<"\nThe answer of Addition is\t"<<c<<"\n\n";
break;
}
case'-':
{
cout<<"enter 1st number\t";
cin>>a;
}
if (option !='+'&&
option != '-'&&
option !='*'&&
option !='/'&&
option !='!')
{
cout<<"you do not enter the correct function key\n\n\n\n";
}
}
EXPERIMENT NO – 04
PROGRAM # 1 Display star before and after your name by using Functions
#include <iostream.h>
Void main ()
{
Void disp ()
Disp();
cout <<``Abrar``;
disp ();
}
Void disp()
{
cout <<``*************``;
}
#include <iostream.h>
Void main ()
{
Int add (intx,inty)
Int a,b;
cout <<``enter 1st number``;
cin>>a ;
cout <<``enter 2nd number``;
cin>>b;
add(a,b);
cout <<``result is``<<add(a,b);
}
int add(int x, int y)
{
Int z;
z=x+y;
return z;
}
#include <iostream.h>
void main ()
{
float a,b;
float fact (float)
cout<<"enter a number for factorial\t";
cin>>a;
fact(a);
b=fact (a);
cout<<"\nfactorial of number is\t"<<b<<"\n";
}
float fact (float a)
{
float z;
z=1;
for (float i=a; i>0; i--)
{
z=z*i;
}
return z;
}
EXPERIMENT NO – 05
PROGRAM # 1 Pointers
#include <iostream.h>
void main ()
{
int a, b;
int * p;
p = &a;
*p = 10;
p = &b;
*p = 20;
cout << "a is " << a << \n;
cout << "b " << b<< \n;
}
PROGRAM # 2 Pointers 2
#include <iostream.h>
void main ()
{
int a = 5, b = 15;
int * p1, * p2;
p1 = &a;
p2 = &b;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 20;
PROGRAM # 3 Pointers 3
#include<iostream.h>
void main ()
{
int var=2;
int *p;
p=&var;
cout<<*p<<"\n";
cout<<var;
}
EXPERIMENT NO – 06
PROGRAM # 1 simple Array
#include<iostream.h>
void main ()
{
int x[5];
x[0]=2;
x[2]=3;
x[3]=4;
x[4]=5;
x[1]=1;
cout<<x[0];
cout<<x[1];
cout<<x[2];
cout<<x[3];
cout<<x[4];
}
a[3]=3;
a[4]=4;
a[5]=5;
for(int i=0;x<6;i=i+2)
cout<<"\n"<<a[i]<<"\n";
}
#include<iostream.h>
void main()
{
int a[6];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
a[5]=5;
for(int j=1;x<6;j=j+2)
cout<<"\n"<<a[j]<<"\n";
}
EXPERIMENT NO – 07
PROGRAM # 1 Defining value one by one in array
#include<iostream.h>
void main()
{
int a[5];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
}
PROGRAM # 3 Defining and printing value one by one in reverse order by using
Array
#include<iostream.h>
void main()
{
int a[5];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
for(int x=4;x>=0;x--)
cout<<"\n\n"<<a[x]<<"\n";
}
#include<iostream.h>
void main()
{
int a[6];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
a[5]=5;
for(int x=0;x<6;x+=2)
cout<<"\n"<<a[x]<<"\n";
}
#include<iostream.h>
void main()
{
int a[6];
a[0]=0;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
a[5]=5;
for(int x=1;x<6;x+=2)
cout<<"\n"<<a[x]<<"\n";
}
EXPERIMENT NO – 08
Program # 1 WRITES A PRGORAM OF ARRAY WITH POINTER TO PRINT OUT
THE ARRAY ELEMENTS IN DESCENDING ORDER
#include<iostream.h>
#include<conio.h>
void main()
{
void clrscr();
int x[5], *p;
x[0]=0;
x[1]=1;
x[2]=2;
x[3]=3;
x[4]=4;
p= &x[4];
for (int i=0; i<5; i++)
{
cout<<*p<<endl;
p--;
}
}
#include<iostream.h>
#include<conio.h>
void main()
{
void clrscr();
int x[5], *p;
x[0]=0;
x[1]=1;
x[2]=2;
x[3]=3;
x[4]=4;
p= &x[0];
for (int i=0; i<5; i++)
{
cout<<*p<<endl;
p++;
}
}
Program # 3 USING ARRAY WITH POINTER TO PRINT OUT THE ARRAY ODD
ELEMENTS
#include<iostream.h>
#include<conio.h>
void main()
{
void clrscr();
int x[5], *p;
x[0]=0;
x[1]=1;
x[2]=2;
x[3]=3;
x[4]=4;
p= &x[0];
for (int i=0; i<3; i++)
{
cout<<*p<<endl;
p=p+2;
}
}
Program # 4 Using array with pointer to input in pointer and than saved in array
#include<iostream.h>
#include<conio.h>
void main()
{
void clrscr();
int x[5], *p;
p=&x[0];
for (int i=0; i<5; i++)
{
cin>>*p;
p++; }}
EXPERIMENT NO – 09
Program # 1 Pointers
#include <iostream.h>
int main ()
{
int numbers[5];
int * p;
p = numbers;
*p = 10;
p++;
*p = 20;
p = &numbers[2];
*p = 30;
p = numbers + 3;
*p = 40;
p = numbers;
*(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
#include <iostream.h>
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<"addition "<<n<<"\nminus "<<m;
return 0;
}
EXPERIMENT NO – 10
Program # 1 illustrating structure and its use
#include <math.h>
#include <iostream.h>
// define the structures
struct DataStructure
{
float radius;
double angle;
};
struct ResultStructure
{
float area;
double sine;
double cosine;
double tangent;
};
ResultStructure compute(struct DataStructure mystruct);
int main ()
{
DataStructure input;
ResultStructure output;
input.radius = 3;
input.angle = 0.8;
output = compute(input);
cout << " The area is "<< output.area << "\n";
cout << " The sine of the angle is " << output.sine << "\n";
cout << " The cosine of the angle is " << output.cosine << "\n";
cout << " The tangent of the angle is " << output.tangent << "\n";
return 0;
}
ResultStructure compute(struct DataStructure mystruct)
{
ResultStructure answer;
answer.area = pow(mystruct.radius,2);
answer.sine = sin(mystruct.angle);
answer.cosine = cos(mystruct.angle);
answer.tangent = tan(mystruct.angle);
return answer;
};
Program # 2 structures
#include <iostream.h>
struct ResultStructure
{
float base;
float height;
float area;
};
cout << "The area is " << myResultStructure.area << " \n";
return 0;
}
void f(ResultStructure &area)
{
area.area = .5f * (area.base * area.height);
}
EXPERIMENT NO – 11
Program #1
#include <iostream.h>
// First definition and use of C++ class
/* This is a minimal class definition
and use in C++ - showing the most basic of
structure.
*/
class Hotel
{
public:
int roomcount;
float occrate;
};
int main ()
{
Hotel room;
Hotel suits;
room.roomcount = 6;
suits.roomcount = 18;
room.occrate = 0.85;
suits.occrate = 0.35;
int totrooms = room.roomcount + suits.roomcount;
cout << "Total rooms listed: " << totrooms << "\n" ;
return 0;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
Program #3
#include <iostream.h>
class myclass
{
public:
int i, j, k; // accessible to entire program
};
int main()
{
myclass a, b;
a.i = 100;
a.j = 4;
a.k = a.i * a.j;
b.k = 12;
cout << a.k << " " << b.k;
return 0;
}
Program #4
#include <iostream.h>
class MyClass
{
public:
int i;
MyClass(int j)
{
i = j;
}
};
int main()
{
MyClass myObject(1);
int *p;
p = &myObject.i; // get address of myObject.i
cout << *p; // access myObject.i via p
return 0;
}
EXPERIMENT NO – 12
Program #1 classes and constructors
#include <iostream.h>
class MyClass
{
public:
int i;
MyClass(int j)
{
i = j;
}
};
int main()
{
MyClass myObject(1);
int *p;
p = &myObject.i; // get address of myObject.i
cout << *p; // access myObject.i via p
return 0;
} // classes example
#include <iostream.h>
class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area ()
{
return (x*y);
}
};
void CRectangle::set_values (int a, int b)
{
x = a;
y = b;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
#include <iostream.h>
class MyClass
{
public:
int i;
MyClass(int j)
{
i = j;
}
};
int main()
{
MyClass myObject(1);
int *p;
p = &myObject.i; // get address of myObject.i
cout << *p; // access myObject.i via p
return 0;
}
#include <iostream.h>
class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area ()
{
return (x*y);
}};
void CRectangle::set_values (int a, int b)
{
x = a;
y = b;
}
int main () {
CRectangle rect, rectb;
rect.set_values (3,4);
rectb.set_values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
#include <iostream.h>
class CRectangle
{
int width, height;
public:
CRectangle (int,int);
int area ()
{
return (width*height);
}};
CRectangle::CRectangle (int a, int b)
{
width = a;
height = b;
}
int main ()
{
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout<<"rect area: "<<rect.area()<<"\n";
cout << "rectb area: " << rectb.area() <<"\n";
return 0;
}
#include <iostream.h>
class MyClass
{
int h;
int i;
public:
MyClass(int j, int k)
{
h = j;
i = k;
}
int getInt()
{
return i;
}
int getHeight()
{
return h;
}
};
int main()
{
MyClass myObject[3] = { MyClass(1, 2), MyClass(3, 4), MyClass(5, 6)};
int i;
for(i=0; i<3; i++)
{
cout << myObject[i].getHeight();
cout << ", ";
cout << myObject[i].getInt() << "\n";
}
return 0;
}
EXPERIMENT NO – 13
Program #1 Use of Class with Scope resolution operator
#include <iostream.h>
class animal
{
protected:
int x;
public:
void eat();
void sleep();
};
class elephant:public animal
{
public:
void trumpet();
};
class dog:public animal
{
public:
void bark()
{
cout<<"barking\n";
}
};
void animal::eat()
{
x=0;
cout<<"eating\n";
}
void animal::sleep()
{
cout<<"sleeping\n";
}
void elephant::trumpet()
{
x=4;
cout<<"trumpeting\n";
}
void main()
{
elephant j;
//j.x=44;
j.sleep();
j.trumpet();
j.eat();
dog k;
k.bark();
EXPERIMENT NO – 14
PROGRAM # 1
IMPLEMENTATION OF INHERITANCE
class sports
{
protected:
float score;
public:
void getscore()
{
cout<< endl<< "Enter your score :";
cin>>score;
}
void putscore()
{
cout<< score<< "\t";
}
};
void main()
{
results s[5];
//clrscr();
for(int i=0;i< 5;i++)
{
s[i].getdata();
s[i].gettest();
s[i].getscore();
}
cout<< "______________________________________________"<< endl;
cout<< endl<< "Name\tRollno\tCP 1\tCP 2\tScore\tTotal"<< endl;
cout<< "----------------------------------------------"<< endl;
for(i=0;i< 5;i++)
{
s[i].putdata();
s[i].puttest();
s[i].putscore();
s[i].putresult();
}
cout<< endl<< "----------------------------------------------";
getch();
}
EXPERIMENT NO – 15
LINK LIST
#include<conio.h>
#include<iostream.h>
class node
{
public:
int info;
node *nxt;
};
node *p,*q,*s;
int ch=0;
void insertfirst()
{
ch=1;
}
void insert()
{
cout<<"Enter Data to link list\n";
p=new node;
cin>> p->info;
p->nxt=NULL;
q->nxt=p;
q=p;
void main()
{
int x;
while (1)
{
cout<<"Enter 1 to add to link list\n";
cin>> x;
// Enter Number
if(x==1)
{
if(ch==0)
insertfirst();
else
insert();
}
// Display
if(x==2)
{
do
{
cout<< s->info<<"\n";
s=s->nxt;
}
while (s->nxt != NULL);
}
}
}
EXPERIMENT NO – 16
VIRTUAL FUNCTION
#include<iostream.h>
class bb
{
public:
virtual void ppp()
{
cout<<"It is the base class"<<endl;
}
};
class d1:public bb
{
public:
void ppp()
{
cout<<"It is the first Derived Class"<<endl;
}
};
class d2:public bb
{
public:
void ppp()
{
cout<<"It is the Second Derived class"<<endl;
}
};
void main()
{
bb *p;
d1 a;
d2 b;
p =&a;
p-> ppp();
}