You are on page 1of 28

Solutions

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

using namespace std;


class details
{
char name[20];
char address[20];
int age;
float salary;

public:

void getData() //by default this function is inline


{

cout<<"Enter the name:";


cin.get(name,20);

getchar(); //to clear the input buffer as cin.get can't ignore nextline char

cout<<"Enter age:";
cin>>age;
getchar();

cout<<"Enter the address:";


cin.get(address,20);

cout<<"Enter salary:";
cin>>salary;
}
void putData();

};//end of class body

//declaring a function inline outside the class


inline void details:: 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>

using namespace std;

//Declaring area functions with different parameters (function overloading)


void area(int);
void area(int,int);
void area(int,int,int);

//defining main() function


int main()
{
cout << "Hello world!" << endl;
area(4,5,6);
area(4,5);
area(14);

return 0;

} //End of main

//Defining area of triangle


void area(int a, int b, int c)
{
float s=(a+b+c)/2;
float ans=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\n area:"<<ans;

} //end of function

//Defining area of rectangle


void area(int l, int b)
{
int ans;
ans=l*b;
cout<<"\n area:"<<ans;
} //end of function

//Defining area of circle


void area(int r)
{
float ans;
ans=pi*r*r;
cout<<"\n area:"<<ans;

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

void matrix::getdata() //get matrix A and B


{
cout<<"Enter row size & column size of the first matrix:";
cin>>row1>>col1;
cout<<"Enter the elements for"<<row1<<"*"<<col1<<"matrix:";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the row size & column size of second matrix:";
cin>>row2>>col2;

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

void matrix::multiply() //function for multiplication


{
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}

void matrix::putdata() // Display results


{
cout<<"resultant matrix is:";
for(i=0;i<row1;i++)
{
cout<<"\n";
for(j=0;j<col2;j++)
{
cout<<"\t"<<c[i][j];
}
}
}

void main() //main function


{
clrscr();
matrix obj;
obj.getdata();
getch();
}
5. Create a class TIME1 with members hours, minutes, seconds. Take input, add two time
objects passing objects to function and display result.

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

s[0].getDetails(); //calling the function using array object of class


s[1].getDetails();
s[2].getDetails();
s[3].getDetails();
s[4].getDetails();

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>

using namespace std;

class factorial{
private:

long int fact;


public:
factorial(int n){
fact=1;
for(int i=1;<i<=n;i++){
fact*=i;
}

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

Complex(int r) // Parameterised constructor for equal values.


{
real=r;
imag=r;
}

Complex(int r,int i) // Parameterised constructor for different values.


{
real=r;
imag=i;
}

Complex(Complex &c) //Copy Constructor.


{
real=c.real;
imag=c.imag;
}

void print()
{
cout<<"\n The sum of two complex nos. is "<<real<<"+"<<imag<<"i.";
}

friend Complex sum(Complex,Complex); //Declaration of friend


function.
};

Complex sum(Complex obj1,Complex obj2)


{
Complex obj3;

obj3.real=obj1.real+obj2.real;
obj3.imag=obj1.imag+obj2.imag;
return obj3;

void main()
{

clrscr();
int a,b,c;

Complex c1; //Calling default constructor.


cout<<"\n\t\tPROGAM TO PERFORM ADDITION OF TWO COMPLEX NUMBERS USING
CONSTRUCTOR OVERLOADING\t\t\n";
cout<<"\n\t\tFor equal values :\t";
cout<<"\n\tEnter the equal value of real and imaginary part of number
1:\n\t";
cin>>a;

Complex c2(a); //Calling parameterised constructor for equal values.

cout<<"\n\t\tFor different values :\t";


cout<<"\n\tEnter the real and imaginary part of number 2:\n\t";
cin>>b>>c;

Complex c3(b,c); //Calling parameterised constructor for different values.


Complex c4=sum(c2,c3); //Calling friend function
Complex c5=c4;
c4.print();
getch();

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

using namespace std;

class biggest

{
private:
int a,b,c,large;
public:

void getdata();

friend int big(biggest);

};

void biggest::getdata()

{
cout<<"Enter any three number"<<endl;
cin>>a>>b>>c;
}

int big(biggest b1)


{
b1.large=b1.a;

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.

// Similar to previous one


12. Write a program to find the greatest of two given numbers in two different classes using friend
function.
CODE::
#include <iostream>

using namespace std;


class second;
class first{
private:
int num1;
public:
first(int n){
num1=n;
}
friend int compare(first,second);
};
class second{
private:
int num2;

public:
second(int n){
num2=n;
}
friend int compare(first,second);
};

int compare(first f,second s){

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,

Return_Type classname :: operator op(Argument list)


{
Function Body
}

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;

cout << "Enter first distance:" << endl;


D1.readDistance();
cout << endl;
cout << "Enter second distance:" << endl;
D2.readDistance();
cout << endl;

//add two distances


D3=D1+D2;

cout << "Total Distance:" << endl;


D3.dispDistance();

cout << endl;


return 0;
}
--------------------------------------------------------------------------------------------------------------------------
LAB 6
--------------------------------------------------------------------------------------------------------------------------
16. Write a program to overload less than (<) operator.

17. Write a program to overload new and delete operators.

CODE:

#include<iostream>
#include<stdlib.h>

using namespace std;


class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
void * p = ::new student();
//void * p = malloc(size); will also work fine

return p;
}

void operator delete(void * p)


{
cout<< "Overloading delete operator " << endl;
free(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.

25. Write a program to illustrate how template functions can be overloaded.


--------------------------------------------------------------------------------------------------------------------------
LAB 9
--------------------------------------------------------------------------------------------------------------------------
26. Write a program to illustrate how to define and declare a class template for reading two data
items from the keyboard and to find their sum.

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.

30. Write a program to copy the contents of a file into another.

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

You might also like