Oop Record
Oop Record
Aim
Algorithm
step-1: Start
step-2: create a function name swap with two parameter
step-3: Assign t=a then a=b then b=t
step-4: After swaping display it
step-5: stop
Program
#include<iostream>
using namespace std;
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<"After Swapping:\nA="<<a<<" B="<<b<<endl;
}
int main()
{
int a,b;
cout<<"Enter Two Number:";
cin>>a>>b;
cout<<"Before Swapping:\nA="<<a<<" B="<<b<<endl;
swap(a,b);
return 0;
}
Output
Enter Two Number:10 20
Before Swapping:
A=10 B=20
After Swapping:
A=20 B=10
Result
Thus the C++ program to swap two numbers using functions was executed
successfully.
Ex No: 1 b MAXIMUM OF NUMBERS USING FUNCTION OVERLOADING
Aim
Algorithm
step-1: start
step-2: Declare all necessary
headers step-3: read a,b
step-4: display “Maximum of two number:”
step-5: display Max(a,b)
step-6: read a,b,c
step-7: display “Maximum of three number:”
step-8: display Max(a,b,c)
step-9: stop
Program
#include<iostream>
using namespace std;
int Max(int a,int b){
return a>b?a:b;
}
int Max(int a,int b,int c){
return Max(Max(a,b),c);
}
int main()
{
int a,b,c;
cout<<"Enter Two Number:";
cin>>a>>b;
cout<<"Maximum of Two number:"<<Max(a,b)<<endl<<endl;
cout<<"Enter Three Number:";
cin>>a>>b>>c;
cout<<"Maximum of Three number:"<<Max(a,b,c)<<endl;
return 0;
}
Output
Enter Two Number:10 20
Maximum of Two number:20
Enter Three Number:100 20 10
Maximum of Three number:100
Result
Thus the C++ program to find maximum of numbers using function overloading was
executed successfully.
Ex No: 2 STUDENT DETAILS USING CLASSES AND OBJECTS
Aim
To write a C++ program to display student details using classes and objects
Algorithm
Step-1: start
Step-2: Declare all necessary headers
Step-3: Create a class name student with data members rollNo as integer and name as
character array
Step-4: Create getData() inside class with public access specifier, inside this function
Read student roll no and name and store it in variable rollNo , name
Step-5: Create displayData() inside class with public access spceifier ,inside this
Function display student’s name and rollno
Step-6: Create an object for the class student as ‘s’
Step-7: call getData() inside class with object ‘s’
Step-8: calldisplayData() inside class with object ‘s’
Step-9: stop
Program
#include<iostream>
using namespace std;
class student
{
private:
char name[25];
int rollNo;
public:
void getData(){
cout<<"Enter your name:";
cin>>name;
cout<<"Enter your roll no:";
cin>>rollNo;
}
Void displayData(){
cout<<"\nStudent Details:"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Roll No:"<<rollNo<<endl;
}
};
int main()
{
student s;
s.getData();
s.displayData();
return 0;
}
Output
Student Details:
Name:Arun
Roll No:10
Result
Thus the C++ program to display student details using classes and object was
executed successfully.
Ex No: 3 CONSTRUCTOR AND DESTRUCTOR
Aim
Algorithm
Step-1: start
Step-8: stop
Program
#include<iostream>
using namespace std;
class Area
{
private:
int ch,w,h;
float r, pi=3.14;
public:
Area()
{
cout<<" Constructor "<<endl;
cout<<"1.Area of cicle\n2.Area of Rectangle\n"
"3.Area of square"<<endl;
cout<<"Enter you choice:";
cin>>ch;
switch(ch){
case 1:circle();break;
case 2:rectangle();break;
case 3:square();break;
default: cout<<"Invalid Choice"<<endl;
}
}
void circle(){
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Are of circle:"<<(pi*r*r)<<endl;
}
void square()
{
cout<<"Enter width of square:";
cin>>w;
cout<<"Area of square:"<<(w*w)<<endl;
}
void rectangle()
{
cout<<"Enter width,height of rectangle:";
cin>>w>>h;
cout<<"Area of rectangle:"<<(w*h)<<endl;
}
~Area()
{
cout<<" Destructor "<<endl;
}
};
int main()
{
Area a;
return 0;
}
Output
Constructor
1.Area of cicle
2.Area of Rectangle
3.Area of square
Enter you choice:1
Enter radius of circle:10
Are of circle:314
Destructor
Result
Thus the C++ program with constructor and destructor was executed successfully.
Ex No: 4 OPERATOR OVERLOADING
Aim
Algorithm
Step-1: start
Step-2: Declare all necessary headers
Step-3: Create a class Complex with data members real and imag
Step-4: create a get() member function inside class with get real and imaginary number
From the user
Step-5: Create a display() member function with display real and imaginary number in
(a+bi) form
Step-6: Create a operloading operator + with return type complex and complex number as
Parameter add both complex number in left side and right side and return the
Resultant as complex number
Step-7: Create an objects c1,c1,add for class Complex
Step-8: call c1.get() and call c2.get()
Step-9: add c1 with c2 and store it in add
Step-10: call c.display()
Step-11: Stop
Program
#include<iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
Complex()
{
real=0;imag=0;
}
void get()
{
cout<<"\nEnter real number:";
cin>>real;
cout<<"Enter imaginary number:";
cin>>imag;
}
Complex operator +(Complex c1)
{
Complex t;
t.real=real+c1.real;
t.imag=imag+c1.imag;
return t;
}
void display()
{
if(imag<0)
{
cout<<"Complex Number: "<<real<<imag<<"i"<<endl;
}
els
e
{ cout<<"Complex Number: "<<real<<"+"<<imag+"i"<<endl;
}
}
};
int main()
{
Complex c1,c2,add;
c1.get();
c2.get();
add=c1+c2;
add.display();
return 0;
}
Output
Result
Thus the C++ program using operator overloading was executed successfully.
Ex No:5 a IMPLICIT TYPE CONVERSION
Aim
To write a C++ program to perform implicit type conversion for various data types.
Algorithm
Step-1: Start
Step-2: Declare all necessary headers
Step-3: Read integer and float value add it and display it’s result
Step-4: Read integer and char value add it and display it’s result
Step-5: Read float and double value add it and display it’s result
Step-6: Stop
Program
#include <iostream>
using namespace
std; int main()
{
int a;
float b;
char c;
double d;
cout<<"Enter int & float value : ";
cin>>a>>b;
cout<<"Addition of int & float:"<<(a+b)<<endl;
cout<<"Enter int & char value :";
cin>>a>>c;
cout<<"Addition of int & char:"<<(a+c)<<endl;
cout<<"Enter float & double value:";
cin>>b>>d;
cout<<"Addition of float & double:"<<(b+d)<<endl;
return 0;
}
Output
Enter int & float value : 10 3.4
Addition of int & float:13.4
Enter int & char value :10 c
Addition of int & char:109
Enter float & double value:1.4 4.3333
Addition of float & double:5.7333
Result
Thus the C++ program to perform implicit type conversion for various data types was
executed successfully.
Ex No: 5 b EXPLICIT TYPE CONVERSION
Aim
To write a C++ program to perform explicit type conversion for various data types.
Algorithm
Step-1: Start
Step-2: Declare all necessary headers
Step-3: Read integer and float value add it with type conversion int in parenthesis and
display it’s result
Step-4: Read integer and char value add it with type conversion char in parenthesis and
display it’s result
Step-5: Read float and double value add it with type conversion double in parenthesis
and display it’s result
Step-6: Stop
Program
#include <iostream>
using namespace
std; int main()
{
int a;
float b;
char c;
double d;
cout<<"Enter int& float value : ";
cin>>a>>b;
cout<<"Addition of int & float:"<<(int)(a+b)<<endl;
cout<<"Enter int & char value :";
cin>>a>>c;
cout<<"Addition of int & char:"<<(char)(a+c)<<endl;
cout<<"Enter float & double value:";
cin>>b>>d;
cout<<"Addition of float & double:"<<(float)(b+d)<<endl;
return 0;
}
Output
Enter int & float value : 10 3.444
Addition of int & float:13
Enter int & char value :20 d
Addition of int & char:x
Enter float & double value:3.44 4.44443
Addition of float & double:7.88443
Result
Thus the C++ program to perform explicit type conversion for various data types was
executed successfully.
Ex No: 6 a SINGLE INHERITANCE
Aim
To write a C++ program for single inheritance.
Algorithm
Step-1: Start
Step-2: Declare all necessary headers
Step-3: Create two class with name Area_Data& Area with Area_Data inherited
Step-4: Area_Data class’s getData() method get width & height of the rectangle
Step-5: Area class’s displayArea() method calculate area of rectangle and display it
Step-6: Create object ‘a’ for Area class and call it’s getData() and displayArea()
Step-7: Stop
Program
#include<iostream>
using namespace std;
class Area_Data
{
protected:
int w,h;
public:
void getData()
{
cout<<"Enter Rectangles width & height:";
cin>>w>>h;
}
};
Class Area:public Area_Data
{
public:
Area()
{
cout<<"Single Inheritance"<<endl;
}
void displayArea()
{
cout<<"Area of rectangle:"<<(h*w);
}
};
int main()
{
Area a;
a.getData();
a.displayArea();
return 0;
}
Output
Single Inheritance
Enter Rectangles width & height:10 30
Area of rectangle:300
Result
Thus the C++ program for single inheritance was executed successfully.
Ex No: 6 b MULTIPLE INHERITANCE
Aim
To write a C++ program for multilevel inheritance.
Algorithm
Step-1: Start
Step-2: Create classes of name base1,base2 and child
Step-3: Create a constructor for class base1,base2 which display it’s class name
Step-4: child class is inherited from the class base1 and base2
Step-5: Create an object ‘c’ for child class
Step-6: Stop
Program
#include<iostream>
using namespace std;
class base1
{
public:
base1()
{
cout<<"Base1 class-1 ";
}
};
class base2
{
public :
base2()
{
cout<<"Base class-2";
}
};
class child:public base1,public base2
{
};
int main()
{
child c;
return 0;
}
Output
Base1 class-1 Base class-2
Result
Thus the C++ program for multiple inheritance was executed successfully.
Ex No: 6 c MULTILEVEL INHERITANCE
Aim
To write a C++ program for multiple inheritance.
Algorithm
Step-1: Start
Step-2: Create three classes name manufacturer , wholesaler and
shop Step-3: Each class contains constructor which display it’s class
name Step-4: Each class is inheritance from another class
Step-5: Create customer class which is derived from shop class
Step-6: Create an object ‘c’ for class customer
Step-7: Stop
Program
#include<iostream>
using namespace std;
class manufacturer
{
public:
manufacturer()
{
cout<<"Manufacturer->";
}
};
class wholesaler:public manufacturer
{
public:
wholesaler()
{
cout<<"Wholesaler->";
}
};
class shop:public wholesaler
{
public:
shop(
)
{ cout<<"Shop->";
}; }
class customer:public shop
{
public:
customer()
{
cout<<"Customer";
}
};
int main()
{
customer product;
return 0;
}
Output
Manufacturer->Wholesaler->Shop->Customer
Result
Thus the C++ program for multilevel inheritance was executed successfully.
Ex No: 6 d HIERARCHICAL INHERITANCE
Aim
To write a C++ program for hierarchical inheritance.
Algorithm
Step-1: Start
Step-2: create three class A,B,C
Step-3: class A has getdata() method which read x,y
Step-4: class B is inherited from class A.this class has product() method which display
product of x & y
Step-5: class C is inherited from class A. this class has sum() method which display sum of
x& y
Step-6: create an object obj1 for class B and obj2 for class
C Step-7: call getdata(),product() from object obj1
Step-8: call getdata(),sum() from object
obj2 Step-9: Stop
Program
#include <iostream>
using namespace
std; class A
{
protected:
int x, y;
public:
void getdata()
{
cout<< "Enter value of x and y:";
cin>> x >> y;
}
};
class B : public A
{
public:
void product()
{
cout<< "Product= " <<(x*y)<<endl<<endl;
}
};
class C : public A
{
public:
void sum()
{
cout<< "Sum= " << (x+y);
}
};
int main()
{
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Output
Result
Thus the C++ program for hierarchical inheritance was executed successfully.
Ex No: 6 e HYBRID INHERITANCE
Aim
To write a C++ program for hybrid inheritance.
Algorithm
Step-1: Start
Step-2: Create class student and it’s constructor will display “student->”
Step-3:Create exam class which is derived from student class and this class display
“Exam+ Step-4:Create class project which display “Project=”
Step-5:Create class result which is derived from exam class and project class this class
Display “Result”
Step-6:Create an object ‘r’ for class result
Step-7:Stop
Program
#include<iostream>
using namespace std;
class student
{
public:
student()
{
cout<<"Student->";
}
};
class exam:public student
{
public:
exam()
{
cout<<"Exam + ";
}
};
class project
{
public:
project()
{
cout<<"Project = ";
}
};
class result:public exam,project
{
public:
result()
{
cout<<"Result";
}
};
int main()
{
result r;
return 0;
}
Output
Result
Thus the C++ program for hybrid inheritance was executed successfully.
Ex No: 6 f MULTIPATH INHERITANCE
Aim
To write a C++ program for multipath inheritance.
Algorithm
Step-1: Start
Step-2: Create four class head,path1,path2,Multipath
Step-3: class head has a display() method which display “this is head class”
Step-4: class path1, path2 are derived class of head and virtual base class each class has
constructor which display it’s class name
Step-5: class Multipath is derived from path1 and path2
Step-6: Create an object ‘m’ for Multipath class and call display() method with dot
operator
Step-7: call display() method with
arrow Step-8: Stop
Program
#include<iostream>
using namespace std;
class Head
{
public:
void display()
{
cout<<"This is head Class"<<endl;
}
};
class path1:virtual public Head
{
public:
path1()
{
cout<<"Path1"<<endl;
}
};
class path2:virtual public Head
{
public:
path2()
{
cout<<"Path2"<<endl;
}
};
class Multipath:public path1,public path2
{
};
int main()
{
Multipath m;
m.display();
return 0;
}
Result
Thus the C++ program for multipath inheritance was executed successfully.
Ex No:7 a DYNAMIC BINDING USING VIRTUAL FUNCTION
Aim
To write a C++ program to construct dynamic binding using virtual function.
Algorithm
Step-1: Start
Step-2:Create two class A,B
Step-3:class A has virtual function display() which display “Base class”
Step-4:class B is derived class of class A and has function display which display ‘Derived
class’
Step-5:Create an pointer object ‘a‘ for base class A and create object ‘b’ for class
B Step-6:assign address of object b to object a
Step-7:call display() method with
arrow Step-8:Stop
Program
#include <iostream>
using namespace
std; class A{
public:
virtual void display(){
cout<< "Base class"<<endl;
}
};
class B:public A{
public:
void display(){
cout<< "Derived Class"<<endl;
}
};
int main()
{
A*a;
B b;
a = &b;
a->display();
return 0;
}
Output
Derived Class
Result
Thus the C++ program to construct dynamic binding using virtual function was
executed successfully.
Ex No:7 b DYNAMIC BINDING USING PURE VIRTUAL FUNCTION
Aim
To write a C++ program to construct dynamic binding using pure virtual function.
Algorithm
Step-1: Start
Step-2: Create two class base and child
Step-3: class base virtual display() method is set to 0
Step-4: class child is derived from base and this class has display() method which display
“Virtual derived class”
Step-5: Create pointer object ‘b’ for class base and create object ‘c’ for class child
Step-6: assign address of object c to object b
Step-7: call display() method from object c with arrow
Step-8: Stop
Program
#include<iostream>
using namespace std;
class base{
public:
virtual void display()=0;
};
Output
Virtual derived class
Result
Thus the C++ program to construct dynamic binding using pure virtual function was
executed successfully.
Ex No: 8 a CLASS TEMPLATE
Aim
Algorithm
Step-1: Start
Step-2: Create T as template class
Step-3: Create class Calculator with data member a,b of T datatype
Step-4: Get values of a,b with parameterized constructor
Step-5: Create function member function add(),subtract(),multiply(),divide() all these
Do addition ,subtraction,multiplication,division based on argument passed
and return it
Step-6: create displayResult() method which display all arithmetic operations with member
Member functions add() ,subtract(),multiply(),divide().
Step-7: Create an object intCal with value(2,1) for class Calculator with type int
Step-8: create an object floatCal with value(2.4,1,2) for class Calculator with type
float Step-9: call displayResult() method from intCal and floatCal object
Step-10:Stop
Program
#include <iostream>
using namespace std;
template<class T>
class Calculator
{
private:
T a,b;
public:
Calculator(T n1, T n2)
{
a = n1;
b = n2;
}
void displayResult()
{
cout<< "Numbers: " << a << " and " << b <<endl;
cout<< a << " + " << b << " = " << add() <<endl;
cout<< a << " - " << b << " = " << subtract() <<endl;
cout<< a << " * " << b << " = " << multiply() <<endl;
cout<< a << " / " << b << " = " << divide() <<endl;
}
T add() { return a + b; }
T subtract() { return a - b; }
T multiply() { return a * b; }
T divide() { return a / b; }
};
int main() {
Calculator<int>intCalc(2, 1);
Calculator<float>floatCalc(2.4, 1.2);
cout<< "Int results:" <<endl;
intCalc.displayResult();
cout<<endl;
cout<< "Float results:" <<endl;
floatCalc.displayResult();
return 0;
}
Output
Int results:
Numbers: 2 and 1
2+1=3
2-1=1
2*1=2
2/1=2
Float results:
Numbers: 2.4 and 1.2
2.4 + 1.2 = 3.6
2.4 - 1.2 = 1.2
2.4 * 1.2 = 2.88
2.4 / 1.2 = 2
Result
Thus the C++ program using class template was executed successfully.
Ex No: 8 b FUNCTION TEMPLATE
Aim
Algorithm
Step-1: Start
Step-2: Declare all necessary headers
Step-3: Create template typename as T
Step-4: Create function Max with parameter a,b with T datatype
Step-5: return a if a>b else return b
Step-6: read a,b of integer datatype and call Max(a.b)
Step-7: read c,d of float datatype and call Max(c,d)
Step-8: read e,f of double datatype and call Max(e,f)
Step-9: Stop
Program
#include<iostream>
using namespace std;
template<typename T>
T Max(T a,T b){
return (a>b)?a:b;
}
int main()
{
int a,b;
float c,d;
double e,f;
cout<<"Enter two int value:";
cin>>a>>b;
cout<<"Maxiumum of two int value:"<<Max(a,b)<<endl;
cout<<"Enter two float value:";
cin>>c>>d;
cout<<"Maxiumum of two float value:"<<Max(c,d)<<endl;
cout<<"Enter two double value:";
cin>>e>>f;
cout<<"Maxiumum of two integer:"<<Max(e,f)<<endl;
return 0;
}
Output
Result
Thus the C++ program using function template was executed successfully.
Ex No: 9 SEQUENTIAL FILE ACCESS
Aim
Algorithm
Step-1: Start
Step-2: Include all necessary header files
Step-3: Create an object file_w of ofstream type and pass location of student.dat file with
Write mode
Step-4: Get student details from user and write it in to file and close file_w
Step-5: create an object file_r of ifstream type and pass location of student.dat file
Step-6: read all details from file and store it in to variable and then display it
Step-7: closefile_r
Step-8: Stop
Program
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string name;
int age;
char grade;
ofstreamfile_w;
file_w.open("student.dat",ios::out);
cout<<"Enter student name:";
cin>>name;
file_w<<name<<endl;
cout<<"Enter student age:";
cin>>age;
file_w<<age<<endl;
cout<<"Enter student grade:";
cin>>grade;
file_w<<grade<<endl;
file_w.close();
ifstreamfile_r;
file_r.open("student.dat",ios::in);
file_r>>name>>age>>grade;
cout<<"\nStudent Details:"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Grade:"<<grade<<endl;
file_r.close();
}
Output
Student Details:
Name:Arun
Age:19
Grade:A
Result
Thus the C++ program to experiment with sequential file access was executed
successfully.
Ex No:10 RANDOM FILE ACCESS
Aim
To write a C++ program to experiment with random access file.
Algorithm
Step-1: Start
Step-2:Create ofstreamfile object name file_w with location of file and write access mode
Step-3: write ‘Hello world ’ on file with file_w object
Step-4: change cursor position -7 step from end and write
“Guys!!’ Step-5: closefile_r object
Step-6:Create ifstream object name file_r with location of file and read access mode
Step-7: set cursor positon to 6th step from beginning read till end and store it to str
Step-8: display str
Step-9:stop
Program
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstreamfile_w("sample.dat",ios::out);
file_w<<"Hello world"<<endl;
file_w.seekp(-7,ios::end);
file_w<<"Guys!!";
file_w.close();
ifstreamfile_r("sample.dat",ios::in);
stringstr;
file_r.seekg(6);
file_r>>str;
cout<<str;
file_r.close();
return 0;
}
Output
Guys!!
Result
Thus the C++ program to experiment with random access file was executed
successfully.
Ex No:11 EXCEPTION HANDLING
Aim
Algorithm
Step-1: Start
Step-2: Include all necessary headers
Step-3: In try block read age from the user
Step-4: if age greater than or equal to 18 then display ‘you are eligible for voting’
Step-5: else throw age
Step-6: catch num if age<18 then display “You are not eligible for voting ‘ and display
That age
Step-7: Stop
Program
#include <iostream>
using namespace
std; int main()
{
int age;
try {
cout<<"Enter your age:";
cin>>age;
if (age >= 18) {
cout<< "You are eligible for voting";
} else {
throw (age);
}
}
catch (int num) {
cout<< "You are not eligible for voting\n";
cout<< "Age is: " <<num;
}
return 0;
}
Output
Result
Thus the C++ program using exception handling mechanism was executed successfull
Ex No: 12 PROGRAM USING STL
Aim
To write a C++ program using STL.
Algorithm
Step-1: Start
Step-2: Include all necessary headers
Step-3: create object vec for vector with int type
Step-4: display vector size before pushing values to it by calling size from vec
Step-5: use for loop and push values from 1-5 to vec vector
Step-6: display vector size after add values by calling size from vec
Step-7: use for loop and display each value from vec object with index value
Step-8: Also use iterator v =vec.begin()
Step-9: In while loop until v != vec.end() display *v and increment
v Step-10: Stop
Program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>vec;
int i;
cout<< "Before the vector size = " <<vec.size() <<endl;
for(i = 1; i < 6; i++)
{
vec.push_back(i);
}
cout<< "extended vector size = " <<vec.size() <<endl;
for(i = 0; i < 5; i++)
{
cout<< "value of vec [" << i << "] = " <<vec[i] <<endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end())
{
cout<< "value of v = " << *v <<endl;
v++;
}
return 0;
}
Output
Result
Thus the C++ program using STL was executed successfully.