0% found this document useful (0 votes)
24 views

C++ W8

The document contains code snippets demonstrating inheritance in C++. It includes examples of single inheritance with classes A, B and C; multiple inheritance with classes StudentsDetails, Marks and C; using base class pointers to call derived class functions; pure virtual functions making a base class abstract; constructor initialization in derived classes; and dynamic casting between base and derived class pointers. The examples show inheritance being used to extend classes with additional properties and methods, call functions through base class pointers, and enforce abstract base classes.

Uploaded by

Fayezee techie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

C++ W8

The document contains code snippets demonstrating inheritance in C++. It includes examples of single inheritance with classes A, B and C; multiple inheritance with classes StudentsDetails, Marks and C; using base class pointers to call derived class functions; pure virtual functions making a base class abstract; constructor initialization in derived classes; and dynamic casting between base and derived class pointers. The examples show inheritance being used to extend classes with additional properties and methods, call functions through base class pointers, and enforce abstract base classes.

Uploaded by

Fayezee techie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1.

#include <iostream>
using namespace std;
class A
{
float base;

public:
void getbase()
{
cout << "Enter value of base: ";
cin >> base;
}
float givebase(void)
{
return base;
}
};
class B
{
float height;

public:
void getHeight()
{
cout << "Enter value of height: ";
cin >> height;
}
float giveheight(void)
{
return height;
}
};
class C : public A, public B
{
float area;

public:
void Areaoftriangle()
{
area = (0.5 )* giveheight()* givebase();
cout << "Area ="<<area;
}
};
int main()
{
C a1;
a1.getbase();
a1.getHeight();
a1.Areaoftriangle();
return 0;
}

OUTPUT:

Q2.
#include <iostream>
using namespace std;

class StudentsDetails
{
string stu_name;
string enroll;

public:
void getDetails()
{
cout << "Enter student name:\n ";
getline(cin, stu_name);
cout << "Enter Enrollment number:\n ";
getline(cin, enroll);
}
};
class Marks
{

public:
int marks[5];
void getmarks()
{
for (int i = 0; i < 5; i++)
{
cout << "Enter value of marks[" << i << "] :";
cin >> marks[i];
}
}
};
class C : public StudentsDetails, public Marks
{
float total;

public:
void gettotal()
{

for (int i = 0; i < 5; i++)


{
total += marks[i];
}
cout << "Total =" << total;
}
};
int main()
{
C m1;
m1.getDetails();
m1.getmarks();
m1.gettotal();
return 0;
}
OUTPUT:

Q3.
int main()
{
base* b;
derived d;
b = &d;
b->calculate();
d.display();
return 0;
}

Q4.
#include <iostream>
using namespace std;
class base_food_items
{
public:
char item_name[20];
int quantity;
int item_price;

public:
virtual void order()
{
cout << "enter item name:";
cin >> item_name;
cout << "enter quantity";
cin >> quantity;
cout << "Item price";
cin >> item_price;
}
void total_price()
{
cout << "order is: " << item_name << "\t"<< "quantity:" <<
quantity << endl;
cout << "total price=" << item_price * quantity << endl;
}
};
class Chinese : public base_food_items
{
public:
void order()
{
cout << "enter item name: ";
cin >> item_name;
cout << "enter quantity ";
cin >> quantity;
cout << "Item price ";
cin >> item_price;
}
void total_price()
{
cout << "order is: " << item_name << "\t"<< "quantity: " <<
quantity << endl;
cout << "total price= " << item_price * quantity << endl;
}
};

int main()
{
base_food_items *A;
Chinese B;
A = &B;
A->order();
B.total_price();
}
OUTPUT:

Q5.
#include <bits/stdc++.h>

using namespace std;


class A
{
public:
virtual void Display1() = 0;
virtual void Display2() = 0;
};

class B : public A
{
public:
void Display1() { cout << "This is Display1() method of Derived class"
<< endl; }
void Display2() { cout << "This is Display2() method of Derived class"
<< endl; }
};
int main()
{

A *a;
B b;
a = &b;
a->Display1();
a->Display2();
return 0;
}
OUTPUT:

Q6.
#include <iostream>
using namespace std;
class A
{
public:
int a, b, c;
A(int a, int b)
{
cout << " constructor called" << endl;
}
};

class B : public A
{
int y;

public:
B(int i, int j) : A(a, b)
{
a = i;
b = j;
c = a + b;

cout << "The Sum = " << c;


}
};

int main()
{
int a, b;
cout << "Enter 2 numbers" << endl;
cin >> a >> b;
B obj(a, b);
return 0;
}

OUTPUT:

Q7.
A) It will give error since we cannot dynamically cast base class to the derived class.

B)It can be simply resolved by making an empty virtual function in the base to make it
abstract.

#include <iostream>
using namespace std;
class Base
{
public:
virtual void function() {}
};
class Derived : public Base
{
};
int main()
{
Base *base_ptr = new Derived;
Derived *derived_ptr = dynamic_cast<Derived *>(base_ptr);
if (derived_ptr != NULL)
cout << "It is working";
else
cout << "cannot cast Base* to Derived*";
return 0;
}

OUTPUT:

You might also like