Oops Lab File
Oops Lab File
--------------------------------------------------------------------------------------------------------------------------
LAB 1
--------------------------------------------------------------------------------------------------------------------------
1. Write a program to take name, address as character array, age as int , salary as float and
contains inline functions to set the values and display it.
INLINE FUNCTIONS: C++ inline function is powerful concept that is commonly used
with classes. If a function is inline, the compiler places a copy of the code of
that function at each point where the function is called at compile time.
CODE:
#include <iostream>
public:
getchar(); //to clear the input buffer as cin.get can't ignore nextline char
cout<<"Enter age:";
cin>>age;
getchar();
cout<<"Enter salary:";
cin>>salary;
}
void putData();
cout<<"\n Name:"<<name<<endl;
cout<<"\n Address:"<<address<<endl;
cout<<"\n Age:"<<age<<endl;
cout<<"\n Salary:"<<salary;
}
//beginning of main
int main() {
details d1;
d1.getData();
d1.putData();
return 0;
}//end of main
2. Using the concept of function overloading Write function for calculating the area of
triangle ,circle and rectangle.
CODE:
#include <iostream>
#define pi 22/7
#include<math.h>
return 0;
} //End of main
} //end of function
} //end of function
3. Write a function power to raise a number m to power n. The function takes a double value
for m and int value for n. Use default value for n to make the function to calculate squares
when this argument is omitted.
CODE:
#include<iostream.h>
#include<math.h>
//Defining power function with default value
void power(double m,int n=2)
{
cout<<"\n"<<m<<"^"<<n<<"="<<pow(m,n);
}
//main function beginning
int main()
{
power(3.45,6);
power(6);
return 0;
} //end of main
--------------------------------------------------------------------------------------------------------------------------
LAB 2
--------------------------------------------------------------------------------------------------------------------------
4. Write a program for multiplication of two matrices using OOP.
CODE:
#include<iostream.h>
#include<conio.h>
//declaring class
class matrix
{
int i,j,a[10][10],b[10][10],c[10][10],row1,row2,col1,col2,k;
public:
void getdata();
void putdata();
void multiply();
};
if(col1==row2)
{
cout<<"Enter the elements for"<<row2<<"*"<<col2<<"matrix:";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>b[i][j];
}
}
multiply();
putdata();
}
else
cout<<"multiplication is not possible";
}
CODE:
#include<iostream>
using namespace std;
class time1
{
int hr,minute,sec;
public:
void get()
{
cin>>hr>>minute>>sec;
}
void disp()
{
cout<<hr<<":"<<minute<<":"<<sec;
}
void sum(time1,time1);
};
void time::sum(time t1,time t2)
{
sec=t1.sec+t2.sec;
minute=sec/60;
sec=sec%60;
minute =minute +t1.minute +t2.minute;
hr=minute /60;
minute =minute %60;
hr=hr+t1.hr+t2.hr;
}
int main()
{
time1 t1,t2,t3;
cout<<"Enter 1st time:";
t1.get();
cout<<"Enter 2nd time:";
t2.get();
cout<<"The 1st time is";
t1.disp();
cout<<"\nThe 2nd time is";
t2.disp();
t3.sum(t1,t2);
cout<<"\nThe resultant time is";
t3.disp();
}
6. Create a class Student which has data members as name, branch, roll no, age, gender,
marks in five subjects. Display the name of the student and his percentage who has more
than 70%.Use array of objects.
#include<iostream.h>
#include<conio.h>
Class student{
private:
char firstName[20], lastName[20],gender,branch;
int rollNo,age,marks[5],total;
public:
int percentage;
void getDetails();
void putDetails();
};//end of class
void student::getDetails()
{
cout<<"Enter First Name:";
cin>>firstName;
cout<<"Enter Last Name:";
cin>>lastName;
cout<<"Enter gender(Male/Female):";
cin>>gender;
cout<<"Enter course(CSE/IT/MAE/ECE/EEE):";
cin>>course;
cout<<"Enter Roll Number";
cin>>rollNo;
total=0;
for(int i=0;i<5;i++)
{ cout<<"Enter Subject "<<i+1<<" marks";
cin>>marks[i];
total+=marks[i];
} //end of for loop
percentage=total/5;
} //end of function getDetails()
void student::putDetails()
{
cout<<"First Name:"<<firstName;
cout<<"Last Name:"<<lastName;
cout<<"Gender:"<<gender;
cout<<"Course:"<<course;
cout<<"Roll Number"<<rollNo;
cout<<"Result(in %)"<<percentage;
}
int main()
{
student s[5]; //declaring class object as array of size 5
for(int i=0;i<5;i++)
{
if(s[i].percentage=>70)
s[i].putDetails();
} //end of for loop
} //end of main
--------------------------------------------------------------------------------------------------------------------------
LAB 3
--------------------------------------------------------------------------------------------------------------------------
7. Write a program to enter any number and find its factorial using constructor.
#include <iostream>
class factorial{
private:
cout<<n<<"!="<<fact;
};
int main()
{
factorial f(5);
}
8. Write a program to perform addition of two complex numbers using constructor overloading.
The first constructor which takes no argument is used to create objects which are not initialized,
second which takes one argument is used to initialize real and imag parts to equal values and third
which takes two argument is used to initialized real and imag to two different values.
CODE:
# include<iostream>
class Complex
{
private:
int real,imag;
public:
Complex() //Default Constructor.
{ real=0; imag=0; }
void print()
{
cout<<"\n The sum of two complex nos. is "<<real<<"+"<<imag<<"i.";
}
obj3.real=obj1.real+obj2.real;
obj3.imag=obj1.imag+obj2.imag;
return obj3;
void main()
{
clrscr();
int a,b,c;
}
9. Write a program to generate a Fibonacci series using a copy constructor.
CODE:
#include<iostream.h>
#include<conio.h>
class series //class series to find fibonacci series
{
private:
int num,a,b,c;
public:
series(int x){
num=x;
}
series(series &y);
};
//Defining copy constructor outside the class
series::series( series &y){
a=0;
b=1;
cout<<"\n the series is :";
cout<<a<<endl;
cout<<b<<endl;
for(i=0;i<=y.num;i++){
c=a+b;
a=b;
b=c;
cout<<c<<endl;
}
}
//main function
void main(){
clrscr();
int z;
cout<<"\n entre the no:"<<endl;
cin>>z;
series A(z),A2;
A2=A; //calling copy constructor
getch();
}
--------------------------------------------------------------------------------------------------------------------------
LAB 4
--------------------------------------------------------------------------------------------------------------------------
10. Write a program to find the biggest of three numbers using friend function.
#include <iostream>
class biggest
{
private:
int a,b,c,large;
public:
void getdata();
};
void biggest::getdata()
{
cout<<"Enter any three number"<<endl;
cin>>a>>b>>c;
}
if(b1.b>b1.large)
{
b1.large=b1.b;
}
if(b1.c>b1.large)
{
b1.large=b1.c;
}
cout<<endl;
cout<<" Biggest no answer is.........="<<b1.large;
return 0;
}
int main()
{
class biggest obj;
clrscr();
obj.getdata();
big(obj);
getch();
return 0;
11. Write a program to demonstrate the use of friend function with Inline assignment.
public:
second(int n){
num2=n;
}
friend int compare(first,second);
};
if(f.num1>s.num2){
return f.num1;
}else{
return s.num2;
}
}
int main(){
first f(18);
second s(20);
cout<<compare(f,s)<<"..is greater";
}
--------------------------------------------------------------------------------------------------------------------------
LAB 5
--------------------------------------------------------------------------------------------------------------------------
13. Write a program to find the sum of two numbers declared in a class and display the numbers
and sum using friend class.
#include<iostream>
class B;
class A{
private:
int num1,num2;
public:
A(int x,int y){
num1=x;
num2=y;
}
friend class B;
}; //end of class
class B{
private:
int sum;
public:
void sum(A);
}
void B::sum(A a){
sum=a.num1+a.num2;
cout<<a.num1<<"+"<<a.num2<<"="<<sum;
int main()
{
A a(4,4);
B b;
b.sum(a);
}
14. Write a program to overload unary increment (++) & (--) operator .
OPERATOR OVERLOADING can be done by declaring the function, its syntax is,
CODE:
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n=x;
}
//function to display number
void dispNum(void)
{
cout << "value of n is: " << n;
}
//unary ++ operator overloading
void operator ++ (void)
{
n=++n;
}
//unary -- operator overloading
void operator -- (void)
{
n=--n;
}
};
int main()
{
NUM num;
num.getNum(10);
++num;
cout << "After increment - ";
num.dispNum();
cout << endl;
--num;
cout << "After decrement - ";
num.dispNum();
cout << endl;
return 0;
}
15. Write a program to overload binary + operator.
CODE:
#include<iostream>
using namespace std;
class Distance
{
private:
int feet,inches;
public:
//function to read distance
void readDistance(void)
{
cout << "Enter feet: ";
cin >>feet;
cout << "Enter inches: ";
cin >>inches;
}
//function to display distance
void dispDistance(void)
{
cout << "Feet:" << feet << "\t" << "Inches:" << inches << endl;
}
//add two Distance using + operator overloading
Distance operator+(Distance &dist1)
{
Distance tempD; //to add two distances
tempD.inches= inches + dist1.inches;
tempD.feet = feet + dist1.feet + (tempD.inches/12);
tempD.inches=tempD.inches%12;
return tempD;
}
};
int main()
{
Distance D1,D2,D3;
CODE:
#include<iostream>
#include<stdlib.h>
return p;
}
int main()
{
student * p = new student("Yash", 24);
p->display();
delete p;
}
18. Create a base class basic_info with data members name ,roll no, gender and two member
functions getdata and display. Derive a class physical_fit from basic_info which has data members
height and weight and member functions getdata and display. Display all the information using
object of derived class.
--------------------------------------------------------------------------------------------------------------------------
LAB 7
--------------------------------------------------------------------------------------------------------------------------
20. Create class first with data members book no, book name and member function getdata and
putdata. Create a class second with data members author name ,publisher and members getdata
and showdata. Derive a class third from first and second with data member no of pages and year of
publication. Display all these information using array of objects of third class.
21. Design three classes STUDENT ,EXAM and RESULT. The STUDENT class has data members such
as rollno, name. create a class EXAM by inheriting the STUDENT class. The EXAM class adds data
members representing the marks scored in six subjects. Derive the RESULT from the EXAM class
and has its own data members such as total marks. Write a program to model this relationship.
22. Create a base class called SHAPE. Use this class to store two double type values. Derive two
specific classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a
member function getdata to initialize base class data members and another member function
display to compute and display the area of figures. Make display a virtual function and redefine this
function in the derived classes to suit their requirements. Using these three classes design a
program that will accept driven of a TRIANGLE or RECTANGLE interactively and display the area.
--------------------------------------------------------------------------------------------------------------------------
LAB 8
--------------------------------------------------------------------------------------------------------------------------
23. Write a program to define the function template for swapping two items of the various data
types such as integer ,float,and characters.
24. Write a program to define the function template for calculating the square of given numbers
with different data types.
27. Write a program to demonstrate the use of special functions, constructor and destructor in the
class template. The program is used to find the biggest of two entered numbers.
28. Write a program to read a set of lines from the keyboard and to store it on a specified file.
--------------------------------------------------------------------------------------------------------------------------
LAB 10
--------------------------------------------------------------------------------------------------------------------------
29. Write a program to read a text file and display its contents on the screen.
31. Write a program to read the class object of student_info such as name , age ,sex ,height and
weight from the keyboard and to store them on a specified file using read() and write() functions.
Again the same file is opened for reading and displaying the contents of the file on the screen.
--END--