0% found this document useful (0 votes)
45 views7 pages

OOPs Lab 6

The document contains code examples demonstrating different types of inheritance in C++, including single, multilevel, hierarchical and multiple inheritance. It also contains examples of inheritance to define classes for shapes (circle, triangle, rectangle), employee types (regular, part-time), account types (savings, current) and demonstrates calculating areas and salaries by inheriting common properties and behaviors into derived classes.

Uploaded by

Salam Saheb
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)
45 views7 pages

OOPs Lab 6

The document contains code examples demonstrating different types of inheritance in C++, including single, multilevel, hierarchical and multiple inheritance. It also contains examples of inheritance to define classes for shapes (circle, triangle, rectangle), employee types (regular, part-time), account types (savings, current) and demonstrates calculating areas and salaries by inheriting common properties and behaviors into derived classes.

Uploaded by

Salam Saheb
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/ 7

Q1. WAP to demonstrate all types of inheritance.

//Single Inheritance
#include <iostream>
using namespace std;
class A {
public:
A(){
cout<<"Constructor of A class"<<endl;
}
};
class B: public A {
public:
B(){
cout<<"Constructor of B class";
}
};
int main() {
//Creating object of class B
B obj;
return 0;
}

//Multilevel inheritance
#include <iostream>
using namespace std;

class A {
public:
void display() {
cout<<"Base class content.";
}
};

class B : public A {};

class C : public B {};

int main() {
C obj;
obj.display();
return 0;
}

//Hierarchical Inheritance 

#include <iostream>
using namespace std;

// base class
class Animal {
public:
void info() {
cout << "I am an animal." << endl;
}
};

// derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I am a Dog. Woof woof." << endl;
}
};

// derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I am a Cat. Meow." << endl;
}
};

int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info(); // Parent Class function
dog1.bark();

// Create object of Cat class


Cat cat1;
cout << "\nCat Class:" << endl;
cat1.info(); // Parent Class function
cat1.meow();

return 0;
}

//Multiple Inheritance
#include <iostream>
using namespace std;

class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};

class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};

class Bat: public Mammal, public WingedAnimal {};


int main() {
Bat b1;
return 0;
}

Q5. Create a class ‘shape’. Derive three classes from it: Circle, Triangle and
Rectangle. Include the relevant data members and functions in all the classes. Find
the area of each shape and display it.

#include<iostream>
using namespace std;

class Shape
{
public: double a,b;
void get_data ()
{
cin>>a>>b;
}
virtual void display_area () = 0;
};

class Triangle:public Shape


{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};

class Rectangle:public Shape


{
public: void display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}
};

class Circle:public Shape


{
public: void display_area ()
{
cout<<"Area of Circle "<<3.14*a*b<<endl;
}
};
int main()
{
Triangle t;
Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_data();
st->display_area();

Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_data();
sr->display_area();

Circle c;
Shape *sc = &c;
cout<<"Enter radius of circle two times(Note enter same radius both times ) : ";
sc->get_data();
sc->display_area();

return 0;
}

Q6. Create a class which stores employee name,id and salary Derive two classes
from ‘Employee’ class: ‘Regular’ and ‘Part-Time’. The ‘Regular’ class stores DA, HRA
and basic salary. The ‘Part-Time’ class stores the number of hours and pay per
hour.
Calculate the salary of a regular employee and a par-time employee.

#include <iostream>
using namespace std;

class Employee{
protected:
string name;
int id;
double salary;
public:

};

class Regular: public Employee{


private:
double DA;
double HRA;
double basic_salary;
public:
Regular(double d, double h, double b){
DA=d;
HRA=h;
basic_salary=b;
}
void display(){
cout<<"\nSalary of the Regular employee is "<<(DA+HRA+basic_salary);
}

};

class PartTime: public Employee{


private:
int number_of_hours;
double pay_per_hour;
public:
PartTime(int n, double p){
number_of_hours=n;
pay_per_hour=p;
}
void display(){
cout<<"\nSalary of the part-time employee is "<<(number_of_hours*pay_per_hour);
}
};

int main()
{
Regular r(2000,3000,10000);
r.display();

PartTime p(8,800);
p.display();

return 0;
}

Q7. Create a class which stores account number, customer name and balance.
Derive two classes from ‘Account’ class: ‘Savings’ and ‘Current’. The ‘Savings’ class
stores minimum balance. The ‘Current’ class stores the over-due amount. Include
member functions in the appropriate class for
-deposit money
-withdraw [For saving account minimum balance should be checked.]
[For current account overdue amount should be calculated.]
-display balance

#include <iostream>
using namespace std;

class Account{
protected:
int account_number;
string customer_name;
double balance=0.0;
public:
void deposit(){
int amount;
cout<<"\nEnter amount to deposit: ";
cin>>amount;
balance=balance+amount;
}
};

class Savings: public Account{


private:
double min_balance=500;

public:
void withdraw(){
int amount;
cout<<"\nEnter amount to withdraw: ";
cin>>amount;
if (amount<min_balance)
cout<<"The minimum amount that can be withdrawn is "<<min_balance;

else
balance=balance-amount;
}
void display(){
cout<<"\nBalance= "<<balance;
}
};

class Current: public Account{


private:
double over_due_amount=500000;

public:
void withdraw(){
int amount;
cout<<"\nEnter amount to withdraw: ";
cin>>amount;
if (amount>over_due_amount)
cout<<"\nThe maximum amount that can be withdrawn is "<<over_due_amount;

else
balance=balance-amount;
}
void display(){
cout<<"\nBalance= "<<balance;
}
};

int main()
{
Account a;
a.deposit();

int c;
cout<<"1. Savings\n2. Current\nEnter your choice:";
cin>>c;

if (c==1){
Savings s;
s.withdraw();
s.display();
}

if (c==2){
Current c;
c.withdraw();
c.display();
}

return 0;
}

You might also like