Vous êtes sur la page 1sur 14

ASSIGNMENT:-3

Q1) Write a C++ program to demonstrate example of hierarchical inheritance to get square, cube and factorial
of a number.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include<iostream>
using namespace std;
class A
{
public:
int n;
void insert()
{ cout<<"Enter the number::"<<endl;
cin>>n;
}
};
class B : public A
{
public:
int i,f=1;
void facto()
{
for(i=1;i<=n;i++)
f=f*i;
cout<<"Factorial::"<<f<<endl;
}
};
class C : public A
{public:
int s;
void square()
{ s=n*n;
cout<<"Square::"<<s<<endl;
} };
class D : public A
{public:
int c;
void cube()
{ c=n*n*n;
cout<<"Cube::"<<c<<endl;
} };
int main()
{ B obj1;
C obj2;
D obj3;
cout<<”||93.3.1 NAME::SAURAV CHATTERJEE SEC:: 3C ROLL::93||”<<endl;
obj1.insert();
obj1.facto();
obj2.insert();
obj2.square();
obj3.insert();
obj3.cube();
return 0;
}
OUTPUT:

Q2) Write a C++ program to merge the elements of two arrays in another array using the concept of multiple
inheritance.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include<iostream>
using namespace std;
class A
{
public:
int n,i,a[50];
void array1()
{
cout<<"||93.3.2 NAME::SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
cout<<"Enter the size of the 1st array:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the array elements::";
cin>>a[i];
} } };
class B
{
public:
int m,i,b[50];
void array2()
{
cout<<"Enter the size of the 2nd array:";
cin>>m;
for(i=0;i<m;i++)
{
cout<<"Enter the array elements::";
cin>>b[i];
} }
};
class C : public B, public A
{
public:
int size,k,merge[100],i;
void array3()
{
size =n+m;
for(i=0; i<n; i++)
merge[i]=a[i];
for(i=0, k=n; k<size && i<m; i++, k++)
merge[k]=b[i];

cout<<"The array after merging is\n";


for ( i = 0; i < size; ++i)
{
cout<<merge[i]<<" ";
} }
};
int main(int argc, char const *argv[])
{
C obj;
obj.array1();
obj.array2();
obj.array3();
return 0;}

OUTPUT:

Q3) Write a C++ program to find the area of a triangle whose sides are 12cm, 15cm and 17cm, using multilevel
inheritance.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP
CODE:
#include<iostream>
#include<math.h>
using namespace std;
class A
{
public:
int a;
void getdata()
{
cout<<"||93.3.3 NAME::SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
cout<<"Enter the 1st length:";
cin>>a;
}
};
class B : public A
{
public:
int b;
void getdata1()
{
cout<<"Enter the 2nd length:";
cin>>b;
}
};
class C : public B
{
public:
int c;
void getdata3()
{
cout<<"Enter the 3rd length:";
cin>>c;
}
void area()
{
int s,area;
s=(a+b+c)/2;
cout<<"Semi-perimeter::"<<s<<endl;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area::"<<area<<endl;
}
};
int main(int argc, char const *argv[])
{
C obj;
obj.getdata();
obj.getdata1();
obj.getdata3();
obj.area();
return 0;
}
OUTPUT:
Q4) Write a C++ program to display employee information using multiple inheritance.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include <iostream>
using namespace std;
class basicInfo
{
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.getline(name,30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};
class deptInfo
{
protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo(void)
{
cout << "Enter Department Name: EDUCATION \n";
cin.ignore(1);
cin.getline(deptName,30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork,30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
};
class employee:private basicInfo, private deptInfo
{
public:
void getEmployeeInfo(void)
{
cout<<"||93.3.4 NAME::SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
cout << "\nEnter employee's basic info: " << endl;
getBasicInfo();
cout << "\nEnter employee's department info: " << endl;
getDeptInfo();
}
void printEmployeeInfo(void)
{
cout << "\nEmployee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl << endl;
cout << "Department Information...:" << endl;
cout << "Department Name: EDUCATION" << endl;
cout << "Assigned Work: " << assignedWork << endl;
cout << "Time to complete work: " << time2complete<< endl;
}
};
int main()
{
employee emp;
emp.getEmployeeInfo();
emp.printEmployeeInfo();
return 0;
}
OUTPUT:

Q5) Read and print employee information with department and pf information using hierarchical inheritance
program in C++.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include <iostream>
using namespace std;
class basicInfo
{
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout<<"||93.3.5 NAME::SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
cout << "Enter Name: ";
cin.ignore(1);
cin.getline(name,30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};
class deptInfo: private basicInfo
{ protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo(void)
{ getBasicInfo();
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName,30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork,30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
void printDeptInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl; //accessing protected data
cout << "Employee ID: " << empId << endl; //accessing protected data
cout << "Gender: " << gender << endl << endl;//accessing protected data
cout << "Department Information...:" << endl;
cout << "Department Name: " << deptName << endl; //accessing protected data
cout << "Assigned Work: " << assignedWork << endl; //accessing protected data
cout << "Time to complete work: " << time2complete<< endl; //accessing protected data

}
};
class loanInfo:private basicInfo
{
protected:
char loanDetails[30];
int loanAmount;
public:
void getLoanInfo(void)
{
getBasicInfo(); //to get basic info of an employee
cout << "Enter Loan Details: ";
cin.ignore(1);
cin.getline(loanDetails,30);
cout << "Enter loan amount: ";
cin >> loanAmount; }
void printLoanInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl << endl;
cout << "Loan Information...:" << endl;
cout << "Loan Details: " << loanDetails << endl;
cout << "Loan Amount : " << loanAmount << endl;
}};
int main()
{ deptInfo objD;
objD.getDeptInfo();
objD.printDeptInfo();
cout << endl << endl ;
loanInfo objL;
objL.getLoanInfo();
objL.printLoanInfo();
}
OUTPUT:

Q6) Read the total expenses of any family and calculate the final expense using multiple inheritance in C++.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include<iostream>
using namespace std;
class Base
{
public:
double loanPayment, insurance, maintenance,monthlyTotal;
void January()
{
cout<<"|| 93.3.6 NAME:SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
cout << "Enter your monthly loan payment: ";
cin >> loanPayment;
cout << "Enter your monthly insurance: ";
cin >> insurance;
cout << "Enter your monthly maintenance expenses: ";
cin >> maintenance;
}
void calc1()
{
monthlyTotal = loanPayment + insurance + maintenance;
cout<<"Expense of this month is Rs:"<<monthlyTotal<<endl<<endl;

}
};
class Child
{
public:
double loanPayment, insurance, maintenance,monthlyTotal1;
void February()
{
cout << "Enter your monthly loan payment: ";
cin >> loanPayment;
cout << "Enter your monthly insurance: ";
cin >> insurance;
cout << "Enter your monthly maintenance expenses: ";
cin >> maintenance;
}
void calc2()
{ monthlyTotal1 = loanPayment + insurance + maintenance;
cout<<"Expense of this month is Rs:"<<monthlyTotal1<<endl<<endl;
}
};
class Total : public Child, public Base
{
public:
double annualTotal,y;
void calc3()
{
y=monthlyTotal1+monthlyTotal;
annualTotal = ((monthlyTotal+monthlyTotal1)/2) * 12;
cout << "****************************************************\n";
cout << "Expense of both the month are Rs" << y << endl << endl;
cout << "Your total annual expenses are: Rs" << annualTotal << endl << endl;
}
};
int main(int argc, char const *argv[])
{
Total obj;
obj.January();
obj.calc1();
obj.February();
obj.calc2();
obj.calc3();
return 0;
}

OUTPUT:

Q7) There exist two parks, one has triangular shape and another one has circular shape and perimeters of both
the parks are same. Find the area of the circular park by writing a C++ program. The sides of the triangles are
58 cm, 70 cm and 48 cm repectievely. Use the concept of any type of inheritance.

ALGORITHM:

1) START
2) DECLARE THE REQUIRED VARIABLES
3) DECLARE THE FUNCTIONS
4) CALL THE FUNCTIONS AS REQUIRED
5) STOP

CODE:

#include<iostream>
using namespace std;

class Perimeter
{
public:
float a=58,b=70,c=48,t,r;
void calc()
{
t=a+b+c;
r=(t/(2*3.14));
}

};
class Area : public Perimeter
{
public:
float f;
void calc1()
{
f=(3.14*r*r);
cout<<"THE AREA OF THE CIRCLE IS:"<<f<<"cm"<<endl;
}
};
int main(int argc, char const *argv[])
{
Area obj;
cout<<"||93.3.7NAME::SAURAV CHATTERJEE SEC::3C ROLL::93||"<<endl;
obj.calc();
obj.calc1();
return 0;
}
OUTPUT:

Vous aimerez peut-être aussi