Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

A lab report on C++ inheritance, with different codes on the various types of inheritance, Lab Reports of Object Oriented Programming

Inheritance in C++: Single Inheritance Multiple Inheritance Multilevel Inheritance Hybrid Inheritance Hierarchical Inheritance

Typology: Lab Reports

2021/2022

Uploaded on 07/31/2023

reet-1
reet-1 🇮🇳

5 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Reet Shinde
Div: A1
Batch: A11
PA-33
Experiment 4
Aim: Write a program in C++ to implement database of people with different
professions e,g engineer, doctor, student, laborer etc. using the concept of
different types of inheritances.
CODE
1) Single Inheritance
#include <iostream>
#include <string>
using namespace std;
class Doctor {
protected:
string name;
string speciality;
public:
void info() {
cout << "Name and speciality " << endl;
cin>> name >> speciality;
}
};
class Database : public Doctor {
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download A lab report on C++ inheritance, with different codes on the various types of inheritance and more Lab Reports Object Oriented Programming in PDF only on Docsity!

Reet Shinde

Div: A

Batch: A

PA- 33

Experiment 4

Aim: Write a program in C++ to implement database of people with different

professions e,g engineer, doctor, student, laborer etc. using the concept of

different types of inheritances.

CODE

1) Single Inheritance #include #include using namespace std; class Doctor { protected: string name; string speciality; public: void info() { cout << "Name and speciality " << endl; cin>> name >> speciality; } }; class Database : public Doctor {

protected: int srno; public: void display() { cout << " sr no to get from database: "<< endl; cin>>srno; } }; int main() { Database a; a.display(); a.Doctor::info(); return 0; }

class doctor { protected: string name_d; int age_d; public: void get_d() { cout<<"Enter name and age "<<endl; cin>>name_d>>age_d; } void display() { cout<<"Name:"<<name_d<<"Age:"<<age_d<<endl; } }; class engineer { protected: string name_e; int age_e; public: void get_e() { cout<<"Enter name and age "<<endl; cin>>name_e>>age_e; } }; class database:public doctor,public engineer { public: void display() { cout<<name_d<<":"<<age_d<<"\t"<<name_e<<":"<<age_e; } }; int main() { database r;

r.get_d(); r.get_e(); r.display(); } OUTPUT

public: void display() { cout<<name_d<<"\t"<<"\t"<<age_d<<endl<<course<<"\t"<<salary; } }; int main() { database r; r.get_d(); r.get_spez(); r.display(); return 0; }

OUTPUT

void get_n() { cout<<"Enter name and age of nurse under "<<name_d<<endl; cin>>name_n>>age_n; } }; class janitor: public doctor { protected: string name_j; int age_j; public: void get_j(){ cout<<"Enter name and age of janitor under "<<name_d<<endl; } void display() { cout<<name_j<<":"<<age_j; } }; int main() { janitor r; r.get_j(); r.get_d(); nurse n; n.get_n(); n.get_d(); n.doctor::display(); } OUTPUT

5) Hybrid Inheritance #include using namespace std; class Science { protected: string stream; int idno; public: void get_s(){ cout<<"Enter field and id"<<endl; cin>>stream>>idno; } }; class doctor:public Science { protected: string name_d; int age_d; public: void get_d() { cout<<"Enter name and age of id"<<idno<<endl; cin>>name_d>>age_d; } }; class engineer { protected: string name_e; int age_e; public: void get_e() { cout<<"Enter name and age "<<endl; cin>>name_e>>age_e; } }; class database:public doctor,public engineer