How to call construction on another inherited class in c++? - c++

I am attempting to write a constructor using inheritance.
Why does mammal class not inherit the animal constructor ?
Also, why can't I overload it ?
PS: constructor takes 4 parameters.
Here is my code :
class animal {
public:
string name;
string diet;
float dailycalories;
float expectedlifetime;
virtual void display() {
}
virtual void calculateExpectedLifeTime() {
}
animal(string nam, string dit, float dacalo, float explife) {
expectedlifetime = 0;
nam = name;
dit = dailycalories;
dacalo = dailycalories;
explife = expectedlifetime;
}
};
class mammal: public animal {
animal(string nam, string dit, float dacalo, float explife) {
}
public:
float brainsize;
};

Here is how you call the parent constructor in the child constructor in C++:
class mammal: public animal{
mammal(string name, string dit, float dacalo,float explife, float brainsize):animal(name, dit, dacalo, explife){
this -> brainsize = brainsize;
}
public:
float brainsize;
};
Hope this answers your question

Related

How to change a class to another class which derive from the same base class?

I have a base class called Animal:
class Animal {
protected:
std::string name;
int age;
int weight;
public:
Animal(const std::string& _name, int _age, int _weight):name(_name),age(_age),weight(_weight) {};
virtual void animal_cange(Animal*) = 0;
};
and from the Animal class derives two sublasses
class Dog : public Animal {
public:
Dog(const std::string& _name, int _age, int _weight) :Animal(_name, _age, _weight) {};
void animal_cange(Animal* poot) override {
this = new Cat(poot->name,poot->age,poot->weight);
}
};
and this
class Cat : public Animal {
public:
Cat(const std::string& _name, int _age, int _weight) :Animal(_name, _age, _weight) {};
void animal_cange(Animal* poot) override {
this = new Dog(name, age, weight);
}
};
I made a virtual funcion in the base class caled virtual void animal_cange(Animal*) = 0; which should change a Dog object to a Cat object if it is called with the object's already existing name, age and weight value and visa versa but it always gives me error like:
assignment to 'this' (anachronism)
a value of type "Cat *" cannot be assigned to an entity of type "Dog *"
protected member "Animal::name" (declared at line 12) is not accessible through a pointer or object
I also tried without animal_change being a virtual function like this:
class Animal {
protected:
std::string name;
int age;
int weight;
public:
Animal(const std::string& _name, int _age, int _weight):name(_name),age(_age),weight(_weight) {};
};
class Dog : public Animal {
public:
Dog(const std::string& _name, int _age, int _weight) :Animal(_name, _age, _weight) {};
void animal_cange() {
this = new Cat(this->name,this->age,this->weight);
}
};
class Cat : public Animal {
public:
Cat(const std::string& _name, int _age, int _weight) :Animal(_name, _age, _weight) {};
void animal_cange() {
*this = new Dog(name, age, weight);
}
};
And the erorrs i get:
this = new Cat(this->name,this->age,this->weight); : "assignment to 'this' (anachronism)" and the entity error
"no operator matches these operands operand types are: Cat = Dog *"
In general, you cannot assign an object to one of a different class - that's what static type system is about. To "change" the dynamic type of a polymorphic object the client code can create another one like this:
Animal* animal = new Dog{}; // actually you should use smart pointers
if (want_to_change) {
delete animal; // prevents a memory leak; smart pointers perform it automatically
animal = new Cat{};
}
If you would like actual animal type choice to happen during initialization, consider using a factory:
class Factory {
public:
// may be static if uses no state, than you can just write a free function
Animal* produce(/* some parameters */) const;
};
Animal* Factory::produce(/* some parameters */) const {
if (should_be_cat(/* depending on the parameters */)) {
return new Cat{};
} else {
return new Dog{};
}
}

Passing derived class to method as argument which is base class with smart pointer

I have read Head First Design Pattern recently. The book shows related codes in Java. However, I try to convert Java codes to C++. At Observer pattern chapter, I got stuck while converting somewhere. Related codes are follows.
class Subject{ //Publisher
public:
virtual void registerObserver(ObserverPtr o) = 0;
virtual void removeObserver(ObserverPtr o) = 0;
virtual void notifyObservers() = 0;
};
using SubjectPtr = std::shared_ptr<Subject>;
Subscriber class interface:
class Observer{
public:
virtual void update(double temp, double humidity, double pressure) = 0;
};
using ObserverPtr = std::shared_ptr<Observer>;
Display Element interface:
class DisplayElement{
public:
virtual void display() = 0;
};
using DisplayElementPtr = std::shared_ptr<DisplayElement>;
and CurrentConditionsDisplay
class CurrentConditionsDisplay : public Observer, public DisplayElement{
SubjectPtr m_weatherData;
double temperature;
double humidity;
public:
CurrentConditionsDisplay(SubjectPtr weatherData);
void update(double temperature, double humidity, double pressure);
void display();
};
using CurrentConditionsDisplayPtr = std::shared_ptr<CurrentConditionsDisplay>;
My question:
At constructor of class CurrentCurrentConditionsDisplay, I would like that Publisher(Subject) registers CurrentCurrentConditionsDisplay.
//constructor
CurrentConditionsDisplay::CurrentConditionsDisplay(SubjectPtr weatherData) :
temperature(0),
humidity(0)
{
m_weatherData = weatherData;
/* How should I pass 'this' ? */
m_weatherData->registerObserver(???????); //parameter type is ObserverPtr.
}
How should 'pointer this' is passed due to the fact that parameter type is ObserverPtr?
I suggest factory method, something like:
std::shared_ptr<CurrentConditionsDisplay>
MakeCurrentConditionsDisplay(SubjectPtr weatherData)
{
auto res = std::make_shared<CurrentConditionsDisplay>(weatherData);
weatherData->registerObserver(res);
return res;
}
If you insist on doing it in the constructor, you might use std::enable_shared_from_this:
class CurrentConditionsDisplay :
public std::enable_shared_from_this<CurrentConditionsDisplay>,
public Observer,
public DisplayElement
{
SubjectPtr m_weatherData;
double temperature = 0;
double humidity = 0;
public:
explicit CurrentConditionsDisplay(SubjectPtr weatherData) :
m_weatherData(weatherData)
{
m_weatherData->registerObserver(shared_from_this());
}
void update(double temperature, double humidity, double pressure) override;
void display() override;
};
std::shared_from_this cannot be called called from constructor

C++ No Appropriate Default Constructor I am lost

I am stuck on a problem involving Polymorphism. My code keeps telling me that there is no default constructor for my class in this case I named creature, even though I did instantiate a constructor that takes a string in the creature class. I have a feeling I am missing something very small here and was hoping someone could help me with this. The code is as follows below.
class Creature
{
public:
Creature(string);
virtual void DoAction() = 0;
virtual void DrawOnScreen() = 0;
protected:
string CreatureName;
};
Creature::Creature(string pname)
{
this->CreatureName = pname;
};
class Monster : public Creature
{
Monster(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Monster::Monster(string pname)
{
this->CreatureName = pname;
};
class Player : public Creature
{
Player(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Player::Player(string pname)
{
this->CreatureName = pname;
}
class WildPig : public Creature
{
WildPig(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
WildPig::WildPig(string pname)
{
this->CreatureName = pname;
}
class Dragon : public Creature
{
Dragon(string CreatureName);
void DoAction();
protected:
string CreatureName;
};
Dragon::Dragon(string pname)
{
this->CreatureName = pname;
}
I only included the classes within this snippet to keep it short and focused on where I believe the problem lies. Any help would be greatly appreciated.
Monster::Monster(string pname)
{
this->CreatureName = pname;
}
is equivalent to
Monster::Monster(string pname) : Creature()
{
this->CreatureName = pname;
}
And Creature doesn't have default constructor. You need:
Monster::Monster(string pname) : Creature(pname) {}

How to define the constructor in a child class

I am having difficulty trying to implement a constructor for my child class. I understand the purpose of the constructor is to set the states of the class to the values passed? am I correct in this?
I am getting an error;
no matching function for call to 'superclass'
My question is do I have to link my constructor for a child class to the superclass? what is the relationship in terms of constructors between the two classes?
#include<iostream>
using namespace std;
class Buildings
{
private:
float price, area;
string city;
public:
Buildings(float, float, string);
// Buildings(float, float, float);
void virtual display();
void virtual getprice(float);
void virtual getcity(string);
void virtual getarea(float);
};
Buildings::Buildings(float b_price, float b_area, string b_city):price(b_price), area(b_area), city(b_city)
{
}
void Buildings::display()
{
cout<<"The city, price and area(sqft) of the building are: "<<city<<endl<<price<<endl<<area;
}
void Buildings::getprice(float aprice)
{
price = aprice;//potential error handling
}
void Buildings::getarea(float asize)
{
area = asize;
}
void Buildings::getcity(string acity)
{
city = acity;
}
class Apartment:public Buildings
{
private:
float numtennants;
float rent;
float rentpr;
public:
Apartment(float numres, float numrent, float numrentpr);
void virtual display();
void virtual avgrent(float);
void virtual totrent(float);
void virtual totres(float);
};
Apartment::Apartment(float numres, float numrent, float numrentpr):numtennants(numres),rent(numrent),rentpr(numrentpr)
{}
void Apartment::display()
{
Buildings::display();
}
Buildings doesn't have a default constructor. You must explicitly call the only Buildings constructor that exists, passing along the suitable arguments.
If you want do disallow public default-construction of Buildings objects, but allow child-classes to use it, you can make a default constructor that is protected. Like
class Buildings
{
public:
// Public constructor, only way to construct object of this class
// for the general public
Buildings(float, float, string);
// Other public functions...
protected:
// Default constructor, which initializes the private members
// to some suitable values
// Only usable by child-classes
Buildings()
: price(0), area(0), city("")
{}
private:
float price, area;
string city;
};
You must call the parent class's constructor in your child class's member initializer list.
struct A {
A(int a) : a_(a) {}
int a_;
};
struct B : public A {
B(int a, int b) : A(a), b_(b) {}
int b_;
};

List iterator outside range

I have a problem with the STL list class. I have a base class called Contact, and three derived classes, Friend, Colleague and Acquaintance. Each instance of the derived classes has certain fields which I modify in the fill*Class*Details() function. The problem is that when it reaches the push_back line, my program gives me an error saying list insert iterator outside range. What can that be from?
void Agenda::pushContact(string line, string temp)//function that adds a contact of type temp, containing the fields from line to the list
{
Contact *c;
if(temp=="friend") //the contact to add is of type friend
{
c = new Friend();
fillFriendDetails(c,line);//the details of Friend c will be filled
}
else if(temp=="colleague")
{
c = new Colleague();
fillColleagueDetails(c,line);//the details of Colleague c will be filled
}
else if(temp=="acquaintance")
{
c = new Acquaintance();
fillAcquaintanceDetails(c,line);//the details of Acquaintance c will be filled
}
contactList.push_back(c);
}
Also, the contactList is defined as list <Contact*> contactList;.
Edit: This is how the Contact class (+derived classes) are defined:
class Contact
{
public:
string getFullName() { string fullName;fullName.append(firstName); fullName.append(" ");fullName.append(lastName); return fullName;}
public:
void setFullName(string newFirstName, string newLastName) { firstName = newFirstName; lastName = newLastName;}
public:
string getFirstName() { return firstName;}
public:
void setFirstName(string newFirstName) {firstName = newFirstName;}
public:
string getLastName(){return lastName;}
public:
void setLastName(string newLastName){lastName = newLastName;}
public:
string getPhoneNo(){return phoneNo;}
public:
void setPhoneNo(string newPhoneNo) {phoneNo = newPhoneNo;}
public:
void setType(string newType){type=newType;}
public:
string getType(){return type;}
private:
string firstName;
string lastName;
string phoneNo;
string type;
//SubClass setters and getters
public:
virtual void setDateOfBirth(string birth) {}
virtual string getDateOfBirth() {return 0;}
virtual void setCity (string newCity) {}
virtual string getCity() {return 0;}
virtual void setFaculty (string newFaculty) {}
virtual string getFaculty() {return 0;}
virtual void setGroup (string newGroup) {}
virtual string getGroup() {return 0;}
virtual void setJob (string newJob) {}
virtual string getJob () {return 0;}
};
class Friend : public Contact
{
public:
void setDateOfBirth(string birth) {dateOfBirth=birth;}
public:
string getDateOfBirth() {return dateOfBirth;}
public:
void setCity (string newCity){city=newCity;}
public:
string getCity(){return city;}
private:
string dateOfBirth;
string city; //current city of residence
};
class Colleague : public Contact
{
public:
void setFaculty (string newFaculty){faculty = newFaculty;}
public:
string getFaculty(){return faculty;}
public:
void setGroup (string newGroup){group = newGroup;}
public:
string getGroup(){return group;}
private:
string faculty;
string group;
};
class Acquaintance : public Contact
{
public:
void setJob (string newJob){job=newJob;}
public:
string getJob (){return job;}
private:
string job;
};
It looks like the list is being mismanaged at a different point in the code, because the error implies end is incorrect. Most likely either the list is deleted/out of scope or some incorrect erases were performed on the list elements (say using invalid iterators).