Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

SECOND SEMESTER
OBJECT ORIENTED PROGRAMMING

BASIC ELECTRICAL & ELECTRONICS LAB


DEPARTMENT OF ELECTRICAL ENGINEERING

Prepared By: Checked By: Approved By:

Engr. Zubair Khalid Engr. Saqib Riaz Dr.Rahatullah


Lecturer (Lab) Electrical, Senior Lab Engineer Electrical, HOD,
FUUAST-Islamabad FUUAST-Islamabad FUUAST-Islamabad

Object Oriented Programming & Data Structure 1


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Name: ____________________________________________

Registration No: ____________________________________

Roll No: ___________________________________________

Semester: _________________________________________

Batch: ____________________________________________

Object Oriented Programming & Data Structure 2


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

CONTENTS

EXP No. EXPERIMENTS


• Printing name and star pattern.
• Students of 8th semester Morning..
1 • Sum of two numbers.
• Simple calculator by using if else statements.

• Print name by using for loop.


• Printing name using while loop.
• Printing name using do while loop.
2
• Factorial of Numbers.
• Printing square and cube.

• Conversion in centigrade to other scales.


• Conversion Kelvin to centigrade and Fahrenheit.
• Conversion Fahrenheit centigrade and Kelvin.
• Program to find the smallest number.
3
• Program to find the largest number.
• Program takes the input from user and show the sum of all.
• Simple Calculator by using Switch statement.

• Simple Calculator by using Switch statement.


• Add two numbers by using functions.
4
• Program of factorial by using functions.

• Display Pointers using variables.


• Display variables using pointers.
5
• Pointers in a loop.

• 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.

• Defining value one by one in array


• Defining and printing value one by one
7 • Defining and printing value one by one on even
• Defining and printing value one by one on odd

• 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.

• Use of pointer with functions.


• Using functions without pointers.
9
• Using pointers to pass value as an argument.

• Illustrating structure and its use.


• Structures defining a room.
10 • Structures defining a car.

Object Oriented Programming & Data Structure 3


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering
• Illustrating classes and its use.
• Classes defining a room.
11 • Classes defining a car.
• Understanding PUBLIC data.

• Understanding PRIVATE data.


• Classes and constructors.
12
• Classes with constructors and pointers.

• Illustrating function inheritance.


• Passing values by the use of constructors.
13 • Using class with arrays.
• Using classes with arrays and pointers.

• Implementation Of Inheritance
14 • Multiple Inheritance

• Link List
15

16 • Virtual Function

Object Oriented Programming & Data Structure 4


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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 *";
}

PROGRAM # 2 Students of 8th semester Morning.

#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";

cout<<"\n\t\tAbrar Aziz"<<"\n\t\tMUHAMMAD FAKHAR AZEEM";


cout<<"\n\t\tMALIK AWAIS HAYAT"<<"\n\t\tMUHAMMAD FAROOQ";
cout<<"\n\t\tARSHAD MEHMOOD"<<"\n\t\tADIL NASEEM";
cout<<"\n\t\tZain ul Abdeen"<<"\n\t\tS.MANSOOR HUSSAIN SHAH";
cout<<"\n\t\tTARIQ NOMAAN";
cout<<"\n\t\tSYED ABSAR AHMED"<<"\n\t\tSYED ABSAR KAZMI";
cout<<"\n\t\tSYED SHEHZAD ZIA"<<"\n\t\tASJID MEHMOOD";
cout<<"\n\t\tAWAIS KHAN"<<"\n\t\tGHULAM MURTAZA";
cout<<"\n\t\tMUHAMMAD SALEEM"<<"\n\t\tMURTAZA ALI";
cout<<"\n\t\tMUHAMMAD IDREES";
}

PROGRAM # 3 Sum of two numbers


#include<iostream.h>
void main()
{
int a,b;
cout<<"THIS PROGRAM IS FOR ADDITION OF TWO NUMBERS:\n\n";
cout<<"ENTER 1st NUMBER";
cin>>a
cout<<"ENTER 2nd NUMBER";
cin>>b;
cout<<"\n"<<a<<" + "<<b<<" = "<<a+b;
}

Object Oriented Programming & Data Structure 5


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

PROGRAM # 4 Simple calculator by using if else statements


#include<iostream.h>
void main()
{
int a,b,c;
cout<<"SIMPLE CALCULATOR\n\n";
cout<<"\n1.ADDITION\n2.MULTIPLICATION\n3.SUBTRACTION\n4.DIVITION\n";
cout<<"ENTER ANY TWO NUMBERS AND THE OPERATION SIGN & PRESS ENTER:
\n";
cin>>a>>b>>c;
if(c==1)
cout<<"\n"<<a<<" + "<<b<<" = "<<a+b;
else if(c==2)
cout<<"\n"<<a<<" * "<<b<<" = "<<a*b;
else if(c==3)
cout<<"\n"<<a<<" - "<<b<<" = "<<a-b;
else if(c==4)
cout<<"\n"<<a<<" / "<<b<<" = "<<a/b;
}

Object Oriented Programming & Data Structure 6


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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";
}

PROGRAM # 2 Printing name using while loop


#include<iostream.h>
void main()
{
int a=5;
while(a>=1)
{
cout<<"\nAbrar";
a--;
}
}

PROGRAM # 3 Printing name using do while loop


#include<iostream.h>
void main()
{
int a=0;
do
{
cout<<"\nABRAR";
a++;
}
while(a<=4);
}

PROGRAM # 4 Factorial of Numbers


#include <iostream.h>
void main ()
{
int a,fact,i;
cout<<"enter the number for factorial\t\t";
cin>>a;
fact=1;
for (i=a; i>0;i--)
{
fact=fact*i;
}

Object Oriented Programming & Data Structure 7


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

cout<<"\n\nfactolial is\t\t"<<fact<<"\n";
}

PROGRAM # 5 Printing square and cube


#include <iostream.h>
void main ()
{
for (int i=0; i<11; i++)
{
cout<<"\nenter the number\t";
cin>>i;
if(i<11)
{
cout<<"square of this number is \t"<<i*i;
cout<<"\nqube of this number is\t\t"<<i*i*i;
}
else
{
cout<<"\n\twrong value\n\n";
}
}
}

Object Oriented Programming & Data Structure 8


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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";

PROGRAM # 2 Conversion Kelvin to centigrade and Fahrenheit.

#include <iostream.h>
void main ()
{
float c,f,k;
cout<<"\nTempratuer in Kalven = \t";
cin>>k;
c=k-273;

cout<<"\nTemperatuer in centi grade is =\t"<<c<<"c";


f=c+32*5/9;

cout<<"\ntempratuer in faran hight\t"<<f<<"f\n";


}

PROGRAM # 3 Conversion Fahrenheit to centigrade and Kelvin height.

#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";
}

Object Oriented Programming & Data Structure 9


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

PROGRAM # 4 Program to find the smallest number

#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";
}

PROGRAM # 5 Program to find the largest number

#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;

Object Oriented Programming & Data Structure 10


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

cout<<"\nSum of numbers is\t"<<sum;


}
else
{
cout<<"\nYou enter a number which is greater than 10 plz enter number again";
}
if (b<a)
{
b=0;
b=a;
cout<<"\nGreater number which u enter\t"<<b;
}
}
}

PROGRAM # 7 Simple Calculator by using Switch statement

#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;

Object Oriented Programming & Data Structure 11


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

cout<<"enter 2nd number\t";


cin>>b;
c=a-b;
cout<<"\nThe answer of Subtraction is\t"<<c<<"\n\n";
break;
}
case'/':
{
cout<<"enter 1st number\t";
cin>>a;
cout<<"enter 2nd number\t";
cin>>b;
c=a/b;
cout<<"\nThe answer of Division is\t"<<c<<"\n\n";
break;
}
case '*':
{
cout<<"enter 1st number\t";
cin>>a;
cout<<"enter 2nd number\t";
cin>>b;
c=a*b;
cout<<"\nThe answer of Multiplication is\t"<<c<<"\n\n";
break;
}
case'!':
cout<<"enter the number for factorial\t\t";
cin>>a;
fact=1;
for (i=a; i>0;i--)
{
fact=fact*i;
}
cout<<"\n\nfactolial is\t\t"<<fact<<"\n\n";

}
if (option !='+'&&
option != '-'&&
option !='*'&&
option !='/'&&
option !='!')
{
cout<<"you do not enter the correct function key\n\n\n\n";
}
}

Object Oriented Programming & Data Structure 12


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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 <<``*************``;
}

PROGRAM # 2 add two numbers by using functions

#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;
}

Object Oriented Programming & Data Structure 13


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

PROGRAM # 3 Program of factorial by using functions

#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;
}

Object Oriented Programming & Data Structure 14


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;

cout << "a is " << a << \n;


cout << "b is " << b << \n;
}

PROGRAM # 3 Pointers 3

#include<iostream.h>
void main ()
{
int var=2;
int *p;
p=&var;
cout<<*p<<"\n";
cout<<var;
}

Object Oriented Programming & Data Structure 15


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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];
}

PROGRAM # 2 Array and pointers


#include<iostream.h>
void main()
{
int a[5];
int *b,*c,*d,*e,*f;
b=&a[0];
c=&a[1];
d=&a[2];
e=&a[3];
f=&a[4];
*b=1;
*c=2;
*d=3;
*e=4;
*f=5;
for(int x=0;x<5;x++)
cout<<"\n"<<a[x]<<"\n";
}

PROGRAM # 3 Display Even values using array


#include<iostream.h>
void main()
{
int a[6];
a[0]=0;
a[1]=1;
a[2]=2;

Object Oriented Programming & Data Structure 16


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

a[3]=3;
a[4]=4;
a[5]=5;
for(int i=0;x<6;i=i+2)
cout<<"\n"<<a[i]<<"\n";
}

PROGRAM # 4 Display odd values using array

#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";
}

PROGRAM # 5 Using array with pointer printing numbers in reverse order


#include<iostream.h>
void main()
{
int a[5];
int *b,*c,*d,*e,*f;
b=&a[0];
c=&a[1];
d=&a[2];
e=&a[3];
f=&a[4];
*b=1;
*c=2;
*d=3;
*e=4;
*f=5;
for(int x=4;x>=0;x--)
cout<<"\n"<<a[x]<<"\n";
}

Object Oriented Programming & Data Structure 17


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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 # 2 Defining and printing value one by one


#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=0;x<=4;x++)
cout<<"\n"<<a[x]<<"\n";
}

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";
}

Object Oriented Programming & Data Structure 18


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

PROGRAM # 4 Defining and printing value one by one on even locations

#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";
}

PROGRAM # 5 Defining and printing value one by one on odd locations

#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";
}

Object Oriented Programming & Data Structure 19


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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--;
}
}

Program # 2 USING ARRAY WITH POINTER TO SHOW ELEMENTS OF ARRAY


IN ASSCENDING 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[0];
for (int i=0; i<5; i++)
{
cout<<*p<<endl;
p++;
}
}

Object Oriented Programming & Data Structure 20


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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++; }}

Object Oriented Programming & Data Structure 21


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;
}

Object Oriented Programming & Data Structure 22


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Program # 2 complex use of pointer with functions

#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;
}

Object Oriented Programming & Data Structure 23


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;
};

Object Oriented Programming & Data Structure 24


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Program # 2 structures
#include <iostream.h>
struct ResultStructure
{
float base;
float height;
float area;
};

void f(struct ResultStructure &area);


int main ()
{
ResultStructure myResultStructure;
myResultStructure.base = 12;
myResultStructure.height = 23;
f(myResultStructure);

cout << "The area is " << myResultStructure.area << " \n";
return 0;
}
void f(ResultStructure &area)
{
area.area = .5f * (area.base * area.height);
}

Object Oriented Programming & Data Structure 25


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;
}

Program #2 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;
}

Object Oriented Programming & Data Structure 26


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;
}

Object Oriented Programming & Data Structure 27


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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;
}

Object Oriented Programming & Data Structure 28


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Program #2 classes with constructors and pointers

#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;
}

Program #3 function inheritance

#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;
}

Object Oriented Programming & Data Structure 29


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Program #4 passing values by the use of constructors

#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;
}

Object Oriented Programming & Data Structure 30


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

Program #5 using class with arrays

#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;
}

Object Oriented Programming & Data Structure 31


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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();

Object Oriented Programming & Data Structure 32


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

EXPERIMENT NO - 14

PROGRAM # 1

IMPLEMENTATION OF INHERITANCE

/******** IMPLEMENTATION OF HYBRID INHERITANCE ********/


#include< iostream.h>
#include< conio.h>
class student
{
private :
int rn;
char na[20];
public:
void getdata()
{
cout<< "Enter Name And Roll No : ";
cin>>na>>rn;
}
void putdata()
{
cout<< endl<< na<< "\t"<< rn<< "\t";
}
};

class test : public student


{
protected:
float m1,m2;
public:
void gettest()
{
cout<< endl<< "Enter your marks In CP 1 And Cp 2 :";
cin>>m1>>m2;
}
void puttest()
{
cout<< m1<< "\t"<< m2<< "\t";
}
};

class sports
{
protected:
float score;
public:
void getscore()

Object Oriented Programming & Data Structure 33


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

{
cout<< endl<< "Enter your score :";
cin>>score;
}
void putscore()
{
cout<< score<< "\t";
}
};

class results : public test , public sports


{
private :
float total;
public :
void putresult()
{
total = m1+m2+score;
cout<< total;
}
};

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();
}

Object Oriented Programming & Data Structure 34


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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()
{

cout<<"Enter DATA to link list\n";


p=new node;
q=p;
s=p;
cin>> p->info;
p->nxt=NULL;

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)

Object Oriented Programming & Data Structure 35


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

{
cout<<"Enter 1 to add to link list\n";

cout<<"Enter 2 to View 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);
}

}
}

Object Oriented Programming & Data Structure 36


Federal Urdu University of Arts, Science & Technology Islamabad - Pakistan Electrical Engineering

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();
}

Object Oriented Programming & Data Structure 37

You might also like