OOPs Lab 6
OOPs Lab 6
//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.";
}
};
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();
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;
}
};
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;
};
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:
};
};
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;
}
};
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;
}
};
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;
}