C++ W8
C++ W8
#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()
{
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>
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;
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: