I want to know whether one of the following two class design choices is superior in terms of performance and/or maintainability or other concerns which would favor one approach over the other.
First approach:
The base class (parent) has a private data member and one pure virtual void function. The child class(childA) passes the parameter from it's default constructor to the parent and uses the parent's getter method to print out some other value.
Second approach:
The base class (oncle) consists only of the a virual destructor and a pure virtual method. The child class (nephew) has the stores the value as a private data member and has it's own getter function to perform some calculation.
Here is some code:
#include <iostream>
class parent{
public:
parent(int x){ _x = x; }
virtual ~parent(){}
virtual void calc( void ) = 0;
int getX( void ) { return _x; }
private:
int _x;
};
class childA: public parent{
public:
childA(int x): parent(x){}
void calc( void ){std::cout << "x sq: " << parent::getX()*parent::getX() << std::endl;}
};
class oncle{
public:
virtual ~oncle(){}
virtual void calc( void ) = 0;
};
class nephewA: public oncle{
public:
nephewA(int x): _x(x){}
void calc( void ){std::cout << "x sq: " << getX()*getX() << std::endl;}
int getX( void ){ return _x; }
private:
int _x;
};
int main(int argc, char *argv[])
{
parent *first = new childA(3);
parent *second = new childB(3);
first->calc();
second->calc();
oncle *foo = new nephewA(3);
oncle *bar = new nephewB(3);
foo->calc();
bar->calc();
delete bar;
delete foo;
delete second;
delete first;
return 0;
}
In short: the first approach puts the emphasis on the parent and the second on the child class.
So are there any quantifiable aspects which could help me select one of the two approaches?
Aucun commentaire:
Enregistrer un commentaire