How are derived classes and base classes related


Assignment:

How are derived classes and base classes related in these exercises:

Q1. What is wrong with the following code?

class CBadClass
{
private:
int len;
char* p;
public:
CBadClass(const char* str): p(str), len(strlen(p)) {}
CBadClass(){}
};

Q2. Suppose you have a class CBird, as follows, that you want to use as a base class for deriving a hierarchy of bird classes:

class CBird
{
protected:
int wingSpan;
int eggSize;
int airSpeed;
int altitude;
public:
virtual void fl y() { altitude = 100; }
};

Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds. Any guidance will be good help.

Q3. Given the following class,

class CBase
{
protected:
int m_anInt;
public:
CBase(int n): m_anInt(n) { cout < < "Base constructor\n"; }
virtual void Print() const = 0;
};

What sort of class is CBase and why? Derive a class from CBase that sets its inherited integer value, m_anInt, when constructed, and prints it on request. Write a test program to verify that your class is correct.

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: How are derived classes and base classes related
Reference No:- TGS01939267

Now Priced at $25 (50% Discount)

Recommended (95%)

Rated (4.7/5)