0% found this document useful (0 votes)
6 views10 pages

Practical CPP (9-12)

Uploaded by

bansalmadhur57
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)
6 views10 pages

Practical CPP (9-12)

Uploaded by

bansalmadhur57
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/ 10

PRACTICAL -10

(Q: Write a C++ program that illustrate single inheritance . )

CODE:

#include <iostream>
using namespace std;

class Hello {
public:
void displayBase() {
cout << "Hello from Base Class" << endl;
}
};

class Hello2 : public Hello {


public:
void displayDerived() {
cout << "Hello from Derived Class" << endl;
}
};

int main() {
Hello2 obj;
obj.displayBase();
obj.displayDerived();
return 0;
}

OUTPUT:
PRACTICAL -11
(Q: Write a C++ program that illustrate multiple inheritance .)

CODE:

#include <iostream>
using namespace std;

class Clg {
protected:
string name;
public:
void setCollegeName(string x) {
name = x;
}
void displayCollege() {
cout << "College Name: " << name << endl;
}
};
class Div {
protected:
string div_;
public:
void setDivisionName(string y) {
div_ = y;
}
void displayDivision() {
cout << "Division: " << div_ << endl;
}
};

class Student : public Clg, public Div {


private:
string std_name;
public:
void setStudentName(string z) {
std_name = z;
}
void displayStudent() {
cout << "Student Name: " << std_name << endl;
displayCollege();
displayDivision();
}
};
int main() {
Student student;
student.setStudentName("Madhur");
student.setCollegeName("Tecnia");
student.setDivisionName("Div-B")
student.displayStudent();
return 0;
}

OUTPUT:
PRACTICAL -12
(Q: Write a C++ program that illustrate multi-level inheritance . )

CODE:
#include <iostream>
using namespace std;

class Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
void display() {
cout << "Name: " << name << endl;
}
};

class Student : public Person {


public:
void display() {
Person::display();
cout << "Roll Number: " << "60" << endl;
}
};

class Graduate : public Student {


public:
void display() {
Student::display(); // Call derived class display
cout << "BCA MORNING SHIFT" << endl;
}
};
int main() {
Graduate grad;
grad.setName("Madhur Bansal");
grad.display();
return 0;
}
OUTPUT:
PRACTICAL -13
(Q: Write a C++ program that illustrate Hierarchical inheritance . )

CODE:

#include <iostream>
using namespace std;

class Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
void display() {
cout << "Person: " << name << endl;
}
};

class Bro : public Person {


public:
void say() {
cout << name << " says: Hello Bro!" << endl;
}
};

class Bro2 : public Person {


public:
void say2() {
cout << name << " says: Hi" << endl;
}
};
int main() {
Bro a;
Bro2 b;

a.setName("Madhur");
b.setName("kartikay");

a.display();
a.say();
b.display();
b.say2();
return 0;
}

OUTPUT:
PRACTICAL -14
(Q: Write a C++ program to illustrate the order of the exhibition of the
constructor and destructor .)

CODE:

#include <iostream>
using namespace std;
class Base {
public:

Base() {
cout << "Base Constructor" << endl;
}

~Base() {
cout << "Base Destructor" << endl;
}
};

class Derived : public Base {


public:
Derived() {
cout << "Derived Constructor" << endl;
}

~Derived() {
cout << "Derived Destructor" << endl;
}
};
int main() {
Derived obj;
cout << "Madhur Bansal" << endl;
return 0;
}

OUTPUT:
PRACTICAL -15
(Q: Write a program to implement friend function by taking some real-life
example .)

CODE:

#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}

friend void displayPersonDetails(Person p);


};

void displayPersonDetails(Person p) {
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << " years" << endl;
}
int main() {
Person person1("Madhur bansal ,19);
displayPersonDetails(person1);
return 0;
}

OUTPUT:

You might also like