Question: C++ program About Inheritance, Constructors, and Member functions. Tak
ID: 3677867 • Letter: Q
Question
Question: C++ program
About Inheritance, Constructors, and Member functions.
Take a look at the class definitions below and answer the following questions.
class circle
{
private:
double xCoordinate;
double yCoordinate;
double radius;
public:
circle();
circle(double, double, double);
void printInfo() const;
double area();
};
class cylinder:public circle
{
private:
double height;
public:
cylinder();
cylinder(double, double, double, double);
void printInfo() const;
double area();
double volume();
};
What are the headers of the default constructor of the circle and cylinder classes?
What are the headers of the constructor with parameters of circle and cylinder classes?
Which member functions of cylinder class overrides base circle classes member functions?
Write implementation two constructors and two member functions of the circle class given above.
Write implementation two constructors and three member functions of the cylinder class given above.
In your main program, create two objects of circle class called c1 and c2 and two objects of cylinder class called cy1 and cy2, as below. Print info for c1, c2, cy1, and cy2.
class circle
{
private:
double xCoordinate;
double yCoordinate;
double radius;
public:
circle();
circle(double, double, double);
void printInfo() const;
double area();
};
class cylinder:public circle
{
private:
double height;
public:
cylinder();
cylinder(double, double, double, double);
void printInfo() const;
double area();
double volume();
};
Explanation / Answer
Inheritance:
Inheriting the properties from base class to derived class.A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.
#include <iostream>
using namespace std;
// Base class
class Base_class_Name
{
.......
.......
};
// Derived class
class Derived_class_Name: public Base_class_Name
{
.......
.......
};
Constructor:
It initializes the instance of a class, it is also called as special member function.Compiler calss constructor after allocating memory to new object.
Foo myfoo;
Foo myfoo( 3, 54 );
Foo myfoo= Foo( 20, 45 );
Using new with a constructor
Foo* myTest = new Foo();
Foo* myTest = new Foo( 40, 34 );
Member Function:
A member function of a class is a function that has its prototype and difinition within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.
class circle // base class
{
private:
double xCoordinate;
double yCoordinate;
double radius;
public:
circle(){ //default constructor
xCoordinate =0;
yCoordinate =0;
radius = 5;
}
circle(double xp, double yp, double r){//parametrized constructor
xCoordinate =xp;
yCoordinate =yp;
radius = r;
}
void printInfo() {
cout << "XCoordinate :" << xCoordinate << endl;
cout << "YCoordinate : " << yCoordinate << endl;
cout << "Radius: " << radius << endl;
}
double area(){
return 3.14*r*r;
}
};
//Derived class
class cylinder:public circle
{
private:
double height;
public:
cylinder(){//default constructor
super();
height=12;
}
cylinder(double x, double y, double r, double h){//parametrized constructor
super(x,y,r);
height = h;
}
void printInfo(){
super.printInfo();
cout << "Height: " << height << endl;
}//printInfo() of cylinder class overrides base circle classes member //functions printInfo()
double area(){//area() of cylinder class overrides base circle classes member functions area()
return (2*3.14*r*(h+r));
}
double volume(){
return (3.14*r*h*r));
}
};
int main(){
Cricle c1 = new Cricle();
Cricle c2 = new Cricle(0,0,5);
Cylinder cy1 = new Cylinder();
Cylinder cy2 = new Cylinder(1,1,3,4);
c1.printInfo();c2.printInfo();cy1.printInfo();cy2.printInfo();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.