Sei sulla pagina 1di 33

LAB MANUAL

Object oriented Paradigm Lab


MCAS1231

School of Computing Science & Engineering


Version : 1 Date: 04.01.2019 Drafted by :LALIT Sharma

1
LIST OF EXPERIMENTS

Sr.No Topic Pg.No


1. Write a Simple C++ programs to implement various control structures.
a)Switch Statement b)if statement
c)For loop d)While loop
2. Write program on Constructors & destructors.
3. Write a program explaining various access modifiers
4. Write a program for implementing Inline functions
5. Write a c++ Program to understand structure & unions
6 Write a Program to understand friend function & friend Class
7 Write a program in c++ to understand array of objects
8 Write a program for the use of “this” pointer using class
9. Write a program to explain the concept of function overloading
10. Write a program to explain the concept of Constructor overloading
11. Write a program to explain the concept of operator overloading.
12. Write a program for the concept of virtual functions.
13. Write a program to understand the concept of inheritance .
Value Added Experiments

14 Programs to Overload Unary & Binary Operators as Member Function & Non Member
Function, considered as Binary operator as non-member function

15 Programs on Class Templates

Course-Co-ordinator

EXPERIMENTAL SETUP DETAILS FOR THE COURSE


Software Requirements
Any C++ compilers

2
Hardware Requirements
No specific requirements. Any computer Hardware capable of running DOS can be
used for this course.

3
Experiment 1
Title Write a Simple C++ programs to implement various control structures.
a)Switch Statement b)if statement
c)For loop d)While loop

Objective a) To implement switch case for arithmetic operations in C++


Algorithm: step 1 : Start
step 2 : Define variable(Mul, Div, Add, Sub)
step 3 : Input An Character
step 4 : Perform the Airthmatic Operation According to input character(+,-,*,/)
step 5 : Display the Result
step 6 : Stop the Execution

Sample code:
# include <iostream>
using namespace std;

int main()
{
char op;
float num1, num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default: // If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}

4
Sample
Output Enter operator either + or - or * or divide : -
Enter two operands:
3.4
8.4

Experiment No:1.b

Title Write a Simple C++ programs to implement various control structures.


b)if statement

Objective To implement if statement to find largest of the three numbers in C++

Algorithm: Step 1: Start


Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Sample Code:
#include<iostream>
using namespace std;

int main()
{
int num1, num2, num3;
cout << "Enter three numbers: "<<endl;

cin >> num1 >> num2 >> num3;


if(num1 >= num2)
{
if(num1 >= num3)
cout <<num1 << " is the largest number";
else
cout <<num3 << " is the largest number";
}

5
else if(num2 >= num3)
cout <<num2 << " is the largest number";

else
cout <<num3 << " is the largest number";

return 0;
}
Sample
Output Enter the three numbers:
80
90
60
90 is the largest

Experiment No:1.c

Title Write a Simple C++ programs to implement various control structures.


b)For Loop
Objective To implement if statement to find sum of digits of a number in C++

Algorithm: Step 1: Get number by user


Step 2: Get the modulus/remainder of the number
Step 3: sum the remainder of the number
Step 4: Divide the number by 10
Step 5: Repeat the step 2 while number is greater than 0.

Sample Code:
#include <iostream>
using namespace std;
int main()
{
int n,sum=0,m;
cout<<"Enter a number: ";
cin>>n;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
cout<<"Sum is= "<<sum<<endl;
return 0;
}

6
Sample Enter a number: 23
output Sum is= 5

Experiment No:1.d

Title Write a Simple C++ programs to implement various control structures.


b)While Loop

Objective To implement While loop to find reverse of a number in C++

Algorithm: Ask to User Enter any Number


Receive Integer value from user
Check Number is Greater than Zero
Find Remainder of number
Use reverse=reverse*10+remainer formula
Find no=no/10
Print Result

Sample Code:
#include<iostream.h>
#include<conio.h>

void main()
{
int no,rev=0,r,a;
clrscr();
cout<<"Enter the num: ";
cin>>no;
a=no;
while(no>0)
{
r=no%10;
rev=rev*10+r;
no=no/10;
}
cout<<"\nReverse of "<<a<<" is: "<<rev;
getch();
}

Sample Enter any num : 964


output Reverse of 164 is 469

7
Experiment No:2

Title Write program on Constructors & destructors.

Objective To implement concept of constructor and destructor using c++


Algorithm: Step 1: Start the program.
Step 2: Create a class as add
Step 3: Create a constructor and perform addition of two numbers.
Step 4: Use destruction function.
Step 5: End of the program

Sample Code:
#include<iostream.h>
#include<conio.h>
class add {
int c;
public:
add(int a, int b); //constructor function
void display();
~add() //destructor function
{ cout<<"Objects are destroyed”<<endl;
}
};
add::add(int a, int b)
{
c=a+b;
}
void add:: display()
{ cout<<”Add of 2 numbers”<<c;
}
void main()
{
int c,d;
clrscr();
cout<< “Enter the value of c and d”<< ”\n”;
cin>>c>>d;
add a1(c,d);
a1.display();
return 0;
}
Sample
Output Enter values of c and d: 2 3
Add of 2 numbers 5

8
Experiment No:3
Title Write a program explaining various access modifiers

Objective To Implement the concept of access modifiers


Algorithm: Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program

Sample Code:
#include<iostream.h>
#include<conio.h>

class line {
public:
inline float mul(float x, float y) {
return (x * y);
}
inline float cube(float x) {
return (x * x * x);
}
};
void main() {
line obj;
float val1, val2;
clrscr();
cout << "Enter two values:";
cin >> val1>>val2;
cout << "\nMultiplication value is:" << obj.mul(val1, val2);
cout << "\n\nCube value is :" << obj.cube(val1) << "\t" << obj.cube(val2);
getch();
}

Sample
Output Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343

Experiment No:4

9
Title Write a program for implementing Inline functions
Objective To Implement the concept of inline functions using c++.
Algorithm: Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program

Sample Code:
#include<iostream.h>
#include<conio.h>

class line {
public:
inline float mul(float x, float y) {
return (x * y);
}
inline float cube(float x) {
return (x * x * x);
}
};

void main() {
line obj;
float val1, val2;
clrscr();
cout << "Enter two values:";
cin >> val1>>val2;
cout << "\nMultiplication value is:" << obj.mul(val1, val2);
cout << "\n\nCube value is :" << obj.cube(val1) << "\t" <<
obj.cube(val2);
getch();
}

Sample
Output Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343

Experiment No:5

10
Title Write a C++ Program to understand structure & unions
Objective To implement the concept of structures and union in c++
Algorithm: Step 1: Start the program.
Step 2: Declare the structure.
Step 3: Declare and define the struct variables.
Step 4: Declare the values.
Step 5: creating variable for union and initializing values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program

Sample #include<iostream.h>
Code struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};

void main()
{

Employee E; //Statement 1
cout << "\nEnter Employee Id : ";
cin >> E.Id;
cout << "\nEnter Employee Name : ";
cin >> E.Name;
cout << "\nEnter Employee Age : ";
cin >> E.Age;
cout << "\nEnter Employee Salary : ";
cin >> E.Salary;
cout << "\n\nEmployee Id : " << E.Id;
cout << "\nEmployee Name : " << E.Name;
cout << "\nEmployee Age : " << E.Age;
cout << "\nEmployee Salary : " << E.Salary;
}
Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000

Employee Id : 1
Employee Name : Kumar
Employee Age : 29

11
Employee Salary : 45000
Union
#include<iostream.h>

union Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};

void main()
{
Employee E;
cout << "\nEnter Employee Id : ";
cin >> E.Id;
cout << "\nEnter Employee Name : ";
cin >> E.Name;
cout << "\nEnter Employee Age : ";
cin >> E.Age;
cout << "\nEnter Employee Salary : ";
cin >> E.Salary;
cout << "\n\nEmployee Id : " << E.Id;
cout << "\nEmployee Name : " << E.Name;
cout << "\nEmployee Age : " << E.Age;
cout << "\nEmployee Salary : " << E.Salary;
}

Output :

Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000

Employee Id : -20536
Employee Name : ?$?$ ?
Employee Age : -20536
Employee Salary : 45000

Experiment No:6

12
Title Write a Program to understand friend function & friend Class

Objective To implement the friend function as a normal function to access the data of the class.

Algorithm: STEP 1: Start the program.


STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function Distance() inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the value.
STEP 7: Stop the program.

Sample * C++ program to demonstrate the working of friend function.*/


code #include <iostream.h>
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
}; // friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}

int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}
Sample output: Distance: 5.
Experiment No:7

Title Write a program in c++ to understand array of objects

13
Objective Implementation of array of objects

Algorithm: #include<iostream.h>
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;

public:
void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;

cout<<"\n\tEnter Employee Name : ";


cin>>Name;

cout<<"\n\tEnter Employee Age : ";


cin>>Age;

cout<<"\n\tEnter Employee Salary : ";


cin>>Salary;
}

void PutData() //Statement 2 : Defining PutData()


{
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
}

};

void main()
{

int i;

Employee E[3]; //Statement 3 : Creating Array of 3 Employees

for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Employee";
E[i].GetData();
}

14
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].PutData();

Output :

Enter details of 1 Employee


Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Age : 29
Enter Employee Salary : 45000

Enter details of 2 Employee


Enter Employee Id : 102
Enter Employee Name : Mukesh
Enter Employee Age : 31
Enter Employee Salary : 51000

Enter details of 3 Employee


Enter Employee Id : 103
Enter Employee Name : Ramesh
Enter Employee Age : 28
Enter Employee Salary : 47000

Details of Employees
101 Suresh 29 45000
102 Mukesh 31 51000
103 Ramesh 28 47000

Experiment No:8
Title Write a program for the use of “this” pointer using class
Objective To understand “this” pointer using class

15
Algorithm: #include<iostream.h>
#include<conio.h>
#include<string.h>

class Student
{

int Roll;
char Name[25];
float Marks;

public:

Student(int R,float Mks,char Nm[]) //Constructor 1


{
Roll = R;
strcpy(Name,Nm);
Marks = Mks;
}

Student(char Name[],float Marks,int Roll) //Constructor 2


{
Roll = Roll;
strcpy(Name,Name);
Marks = Marks;
}

Student(int Roll,char Name[],float Marks) //Constructor 3


{
this->Roll = Roll;
strcpy(this->Name,Name);
this->Marks = Marks;
}

void Display()
{
cout<<"\n\tRoll : "<<Roll;
cout<<"\n\tName : "<<Name;
cout<<"\n\tMarks : "<<Marks;
}
};

void main()
{

16
Student S1(1,89.63,"Sumit");
Student S2("Kumar",78.53,2);
Student S3(3,"Gaurav",68.94);

cout<<"\n\n\tDetails of Student 1 : ";


S1.Display();

cout<<"\n\n\tDetails of Student 2 : ";


S2.Display();

cout<<"\n\n\tDetails of Student 3 : ";


S3.Display();

Output :

Details of Student 1 :
Roll : 1
Name : Sumit
Marks : 89.63

Details of Student 2 :
Roll : 31883
Name : ?&;6#?#?6#N$?%_5$?
Marks : 1.07643e+24

Details of Student 3 :
Roll : 3
Name : Gaurav
Marks : 68.94

Experiment No:9
Title Write a program to explain the concept of function overloading

Objective Implementation of the concept of function overloading

17
Algorithm: #include<iostream.h>
#include<conio.h>

class CalculateArea
{
public:
void Area(int r) //Overloaded Function 1
{
cout<<"\n\tArea of Circle is : "<<3.14*r*r;
}
void Area(int l,int b) //Overloaded Function 2
{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(float l,int b) //Overloaded Function 3


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(int l,float b) //Overloaded Function 4


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

};
void main()
{
CalculateArea C;
C.Area(5); //Statement 1
C.Area(5,3); //Statement 2
C.Area(7,2.1f); //Statement 3
C.Area(4.7f,2); //Statement 4

}
Output :
Area of Circle is : 78.5
Area of Rectangle is : 15
Area of Rectangle is : 14.7
Area of Rectangle is : 29.4

Experiment No:10

Title Write a program to explain the concept of Constructor overloading.

18
Objective Implementation of Constructor overloading using c++.

Algorithm: #include<iostream.h>
#include<conio.h>
#include<string.h>

class Student
{
int Roll;
char Name;
float Marks;

public:

Student(int r,char nm[],float mks) // Constructor 1


{
Roll = r;
strcpy(Name , nm);
Marks = mks;
}

Student(int r,float mks,char nm[]) // Constructor 2


{
Roll = r;
strcpy(Name , nm);
Marks = mks;
}

Student(char nm[],int r,float mks) // Constructor 3


{
Roll = r;
strcpy(Name , nm);
Marks = mks;
}

void Display()
{
cout<<"\n\t"<<Roll<<"\t"<<Name<<"\t"<<Marks;
}

};

void main()
{
Student S1(101,"Kumar",78.53); //Statement 1
Student S2("Sumit",102,89.27); //Statement 2

19
Student S3(103,67.38,"Kunal"); //Statement 3

S1.Display();
S2.Display();
S3.Display();

}
Output :

101 Kumar 78.53


102 Sumit 89.27
103 Kunal 67.38

Experiment No:11
Title Write a program to explain the concept of operator overloading.
Objective Implementation of operator overloading using c++.

20
Algorithm: Binary Operator Overloading
#include<iostream.h>
#include<conio.h>

class Rectangle
{

int L,B;

public:

Rectangle() //Default Constructor


{
L = 0;
B = 0;
}
Rectangle(int x,int y) //Parameterize Constructor
{
L = x;
B = y;
}
Rectangle operator+(Rectangle Rec) //Binary operator
overloading func.
{
Rectangle R;
R.L = L + Rec.L;
R.B = B + Rec.B;
return R;
}
void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects
cout<<"\n\tRectangle 1 : ";
R1.Display();
cout<<"\n\n\tRectangle 2 : ";
R2.Display();
R3 = R1 + R2; Statement 1
cout<<"\n\n\tRectangle 3 : ";
R3.Display();

21
}
Output :
Rectangle 1 :
L:2
B:5
Rectangle 2 :
L:3
B:4
Rectangle 3 :
L:5
B:9
Unary Operator Overloading
#include<iostream.h>
#include<conio.h>
class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}
void operator++() Unary operator overloading func.
{
L+=2;
B+=2;
}

void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}
};
void main()
{
Rectangle R;
//Creating Object
cout<<"\n\tLength Breadth before increment";
R.Display();
R++;
cout<<"\n\n\tLength Breadth after increment";
R.Display();
}

22
Output :
Length Breadth after increment
L:0
B:0
Length Breadth after increment
L:2
B:2

Experiment No:12
Title Write a program for the concept of virtual functions.

23
Objective To understand concept of virtual functions.
Algorithm: #include<iostream.h>
#include<conio.h>

class BaseClass
{

public:
virtual void Display()
{
cout<<"\n\tThis is Display() method of Base Class";
}

void Show()
{
cout<<"\n\tThis is Show() method of Base Class";
}

};

class DerivedClass : public BaseClass


{

public:
void Display()
{
cout<<"\n\tThis is Display() method of Derived Class";
}

void Show()
{
cout<<"\n\tThis is Show() method of Derived Class";
}

};
void main()
{

DerivedClass D;

BaseClass *B; //Creating Base Class Pointer


B = new BaseClass;

B->Display(); //This will invoke Display() method of Base Class


B->Show(); //This will invoke Show() method of Base Class

24
B=&D;

B->Display(); //This will invoke Display() method of Derived


Class
//bcoz Display() method is virtual in Base Class

B->Show(); //This will invoke Show() method of Base Class


//bcoz Show() method is not virtual in Base Class

Output :

This is Display() method of Base Class


This is Show() method of Base Class
This is Display() method of Derived Class
This is Show() method of Base Class

Experiment No:13

Title Write a program to understand the concept of inheritance .

25
Objective Implementation of inheritance.

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

class Employee
{
int Id;
char Name[25];
int Age;
long Salary;

public:
void GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;

cout<<"\n\tEnter Employee Name : ";


cin>>Name;

cout<<"\n\tEnter Employee Age : ";


cin>>Age;

cout<<"\n\tEnter Employee Salary : ";


cin>>Salary;
}

void PutData()
{
cout<<"\n\nEmployee Id : "<<Id;
cout<<"\nEmployee Name : "<<Name;
cout<<"\nEmployee Age : "<<Age;
cout<<"\nEmployee Salary : "<<Salary;
}

};

class Company : public Employee //Statement 1


{
int RegNo;
char CName[25];

public:
void ReadData()

26
{
cout<<"\n\nEnter Registration No. : ";
cin>>RegNo;

cout<<"\nEnter Company Name : ";


cin>>CName;

void WriteData()
{
cout<<"\n\nRegistration No. : "<<RegNo;
cout<<"\nCompany Name : "<<CName;
}

};
void main()
{

Company C; //Statement 2 : Creating Object of Derived Class

C.GetData(); //Statement 3 : Calling Base Class Method()


C.ReadData();

C.PutData(); //Statement 5 : Calling Base Class Method()


C.WriteData();
}

Output :

Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000

Enter Registration No. : 715


Enter Company Name : TutorialDost

Employee Id : 1
Employee Name : Kumar
Employee Age : 29
Employee Salary : 45000
Registration No. : 715
Company Name : TutorialDost

27
Experiment No:14

Title Write a program to understand the concept of Overload Unary & Binary Operators as
Member Function & Non Member Function, considered as Binary operator as non-
member function .

28
Objective Implementation of Overload Unary & Binary Operators as Member Function & Non
Member Function.

Algorithm: Unary operator as member function


#include<iostream.h>
#include<conio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
counter operator++(int)
{
count++;
}
int get_count()
{
return count;
}
counter operator--(int)
{
count--;
}
};
void main()
{
clrscr();
counter c1;
cout<<"Initial No Of People "<<c1.get_count()<<endl;
c1++;
c1++;
c1++;
cout<<"Present No Of People "<<c1.get_count()<<endl;
c1--;
c1--;
cout<<"Present No Of People "<<c1.get_count()<<endl;
getch();
}

Binary Operator as non-member function


#include<iostream.h>
#include<conio.h>
class company
{

29
int sal;
int nop;
public:
friend void print(company &);
company()
{
sal=0;
nop=0;
}
company operator+(company);
company company:: operator-(company c)
{
company temp;
temp.sal=sal-c.sal;
temp.nop=nop-c.nop;
return temp;
}
void set_data(int s, int n)
{
sal=s;
nop=n;
}
};
void print(company &sum)
{
cout<<"Total Salary is :"<<sum.sal<<endl;
cout<<"Total No: of Parts is:"<<sum.nop<<endl;
}
company company:: operator+(company c)
{
company temp;
temp.sal=sal+c.sal;
temp.nop=nop+c.nop;
return temp;
}
void main()
{
clrscr();
int s,n;
company sum,ram,clif,sub;
cout<<"Enter the salary for Ram";
cin>>s;
cout<<"Enter No:Of Parts for Ram:";
cin>>n;
ram.set_data(s,n);
cout<<"Enter the salary for clif";
cin>>s;

30
cout<<"Enter No:Of Parts for clif:";
cin>>n;
clif.set_data(s,n);
sum=ram+clif;
print(sum);
sub=sum-ram;
print(sub);
getch();
}

Experiment No:15

Title Write a program to understand the concept of Class Templates .

Objective Implementation of class templates.

31
Algorithm: Function Template
#include<iostream.h>
#include<conio.h>
template<typename T> void prn_arr(T *arr,int count)
{
for(int i=0;i<count;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void main()
{
clrscr();
const int a_cnt=5;
const int b_cnt=7;
const int c_cnt=6;
int a[a_cnt]={1,2,3,4,5};
double b[b_cnt]={1.1,2.2,3.3,4.4,5.5,6.6,7.7};
char c[c_cnt]="HELLO";
cout<<"Integer Array\n";
prn_arr(a,a_cnt);
cout<<"\nDouble Array\n";
prn_arr(b,b_cnt);
cout<<"\nCharacter Array\n";
prn_arr(c,c_cnt);
getch();
}

Class Template
#include<iostream.h>
#include<conio.h>
template<class T>
class pair
{
T a;
T b;
public:
pair()
{
cin>>a>>b;
}
T get_max();
};
template<class T>
T pair<T>::get_max()
{

32
T ret;
ret=a>b?a:b;
return ret;
}
void main()
{
clrscr();
cout<<"\nEnter 2 Integer Numbers:\n";
pair<int>obj1;
cout<<"Greatest Integer is "<<obj1.get_max()<<endl;
cout<<"\nEnter 2 Float Numbers:\n";
pair<float>obj2;
cout<<"Greatest Float is "<<obj2.get_max()<<endl;
getch();
}

33

Potrebbero piacerti anche