+ 1
In the below program,How to call a base class from its derived class ?
#include <iostream> using namespace std; class Area { public: int calc(int l, int b) { return l*b; } }; class Perimeter { public: int calc(int l, int b) { return 2 * (l + b); } }; /* Rectangle class is derived from classes Area and Perimeter. */ class Rectangle: ________________ { // Inherit the required base classes private: int length, breadth; public: Rectangle(int l, int b) : length(l), breadth(b) {} int area_calc() { /* Calls calc() of class Area and r
2 Answers
+ 4
Why would you have area and perimeter as classes? I think their use is as a function. What is your over all goal?
I think, depending on what your goal is, that you should have one parent class called Shape with the member functions 'area' and 'perimeter'. Like this:
class Shape {
protected:
int area;
int perimeter;
public:
virtual void calc_area() = 0;
virtual void calc_perim() = 0;
};
class Rectangle: public Shape {
private:
int length;
int breadth;
public:
void calc_area() { area = length * breadth; }
....
};
Something like this? Once again, it depends on what your overall goal is. Happy coding!
https://code.sololearn.com/c8L9NNHNDUKO
+ 1
class Rectangle
{
public:
int calc_area(){return l*b;}
int calc_perimeter(){return 2*(l+b); }
private:
int l;
int b;
};
isn't better?
object oriented is very well but we most use enough classes.