These are C Computer Science II programming problem Please
These are C++ ( Computer Science II ) programming problem. Please provide the detailed answer the following problems.
1)
2)
Solution
Lion is a subclass of Animal . Animal is a superclass of Lion. the relationship between Lion and Animal is Inheritance.
#include <iostream>
using namespace std;
class Animal
{
public:
Animal(string name)
{
m_name = name;
cout<<m_name<<endl;
}
protected:
string m_name;
};
class Lion: public Animal
{
public:
Lion(string name): Animal(name){}
};
int main()
{
Lion lion(\"King\");
return 0;
}
output
King
second program
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << \"Total area: \" << Rect.getArea() << endl;
return 0;
}
Total area: 35

