0% found this document useful (0 votes)
4 views11 pages

Ex - 10

The document contains multiple C++ code examples demonstrating the use of classes and inheritance. It includes implementations for employee management, complex number addition, rectangle and box area calculations, and a project manager class that extends a manager class. Each example showcases different object-oriented programming concepts such as constructors, methods, operator overloading, and inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Ex - 10

The document contains multiple C++ code examples demonstrating the use of classes and inheritance. It includes implementations for employee management, complex number addition, rectangle and box area calculations, and a project manager class that extends a manager class. Each example showcases different object-oriented programming concepts such as constructors, methods, operator overloading, and inheritance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <iostream>

using namespace std;

class Employee{

public:

int Emp_id;

string Emp_name;

float Emp_sal;

Employee(int a,string b,float c){

Emp_id=a;

Emp_name=b;

Emp_sal=c;

void display(){

cout<<"Employee ID :"<<Emp_id<<endl;

cout<<"Employee Name :"<<Emp_name<<endl;

cout<<"Employee Salary :"<<Emp_sal<<endl;

};

int main()

Employee e1(123,"Rahul",120000),e2(120,"A",100000);

e1.display();

e2.display();

}
Employee ID :120

Employee Name :A

Employee Salary :100000


#include <iostream>

using namespace std;

class Complex{

public:

float real,imag;

Complex(){

real=0;

imag=0;

Complex(float x,float y){

real=x;

imag=y;

Complex add(Complex c1,Complex c2){

Complex c;

c.real=c1.real+c2.real;

c.imag=c1.imag+c2.imag;

return c;

void show(){

cout<<"sum of complex numbers is "<<real<<"+i"<<imag<<endl;

};

int main()

{
Complex c1(3,5),c2(2,5),c3;

c3=c3.add(c1,c2);

c1.show();

c2.show();

c3.show();

return 0;

sum of complex numbers is 3+i5

sum of complex numbers is 2+i5

sum of complex numbers is 5+i10


#include <iostream>

using namespace std;

class Complex{

public:

float real,imag;

Complex(){

real=0;

imag=0;

Complex(float x,float y){

real=x;

imag=y;

Complex operator+(Complex c1){

Complex c;

c.real=real+c1.real;

c.imag=imag+c1.imag;

return c;

void show(){

cout<<"sum of complex numbers is "<<real<<"+i"<<imag<<endl;

};

int main()

{
Complex c1(3,5),c2(2,5),c3;

c3=c1+c2;

c1.show();

c2.show();

c3.show();

return 0;

sum of complex numbers is 3+i5

sum of complex numbers is 2+i5

sum of complex numbers is 5+i10


#include <iostream>

using namespace std;

class Rectangle {

protected:

double length;

double width;

public:

Rectangle(double l, double w) {

length = l;

width = w;

double area() {

return length * width;

};

class Box : public Rectangle {

private:

double depth;

public:

Box(double l, double w, double d) : Rectangle(l, w) {


depth = d;

double volume() {

return area() * depth;

};

int main() {

Rectangle rect(10.0, 5.0);

cout << "Area of Rectangle: " << rect.area() << endl;

Box box(10.0, 5.0, 3.0);

cout << "Volume of Box: " << box.volume() << endl;

return 0;

Area of Rectangle: 50

Volume of Box: 150

#include <iostream>
#include <string>

using namespace std;

class Employee { private:

int id; string name;

public:

void getData() {

cout << "Enter Employee ID: ";

cin >> id; cin.ignore();

cout << "Enter Employee Name: ";

getline(cin, name);

} void display() {

cout << "Employee ID: " << id << endl;

cout << "Employee Name: " << name << endl;

};

class Manager : public Employee {

private:

double salary;

public:

void getData() { Employee::getData();

cout << "Enter Manager's Salary: ";

cin >> salary;

} void display() {

Employee::display();

cout << "Manager's Salary: " << salary << endl;

};

class ProjectManager : public Manager { private:

int totalExperience; int numberOfProjectsHandled;


public:

void getData() { Manager::getData();

cout << "Enter Total Experience (in years): ";

cin >> totalExperience;

cout << "Enter Number of Projects Handled: ";

cin >> numberOfProjectsHandled;

} void display() { Manager::display();

cout << "Total Experience: " << totalExperience << " years" << endl;

cout << "Number of Projects Handled: " << numberOfProjectsHandled << endl;

};

int main() {

ProjectManager pm;

pm.getData();

cout << endl << "Project Manager Details:" << endl;

pm.display();

return 0;

Enter Employee ID: 101

Enter Employee Name: Alice

Enter Manager's Salary: 75000

Enter Total Experience (years): 10

Enter Number of Projects Handled: 5

Project Manager Details:

Employee ID: 101

Employee Name: Alice

Manager's Salary: 75000.0

Total Experience: 10 years

Number of Projects Handled: 5

You might also like