I have two classes - Mother(Base) and Daughter(Derived). I am inheriting a function from Mother class and trying to override in Daughter class. It looks like that it overrides, but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything. Please note in the Derived class that I am not inheriting : public Mother
Thanks for the help, as always!!!
This is my code
Mother.hpp
#ifndef Mother_hpp
#define Mother_hpp
#include <iostream>
#include <string>
class Mother
{
public:
Mother();
void sayName();
};
Mother.cpp
#include <iostream>
#include <string>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Mother::Mother(){}
void Mother::sayName(){
cout<<"I am Sandy" <<endl;
}
Daughter.hpp
#ifndef Daughter_hpp
#define Daughter_hpp
#include <iostream>
#include "Mother.hpp"
class Daughter : public Mother
{
public:
Daughter();
void sayName();
};
Daughter.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
Daughter::Daughter() : Mother(){}
void Daughter::sayName(){
cout << "my name is sarah" <<endl;
}
Main.cpp
#include <iostream>
#include "Mother.hpp"
#include "Daughter.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
Mother mom;
mom.sayName();
Daughter d;
d.sayName();
return 0;
}
but my confusion is, even though I don't inherit Mother class, the function still works, so how am I inheriting/overriding it? I am very confused as if I'm really inheriting/overriding anything.
you don't really override sayName() of your Mother class because (as you said) Daughter class doesn't inherit it in the first place. That is, you need to inherit a class first in order to be able to override its virtual functions.
your second call to sayName() works because it's a call to a member function of Daughter class, which is totally independent from Mother class. Note that, just having multiple independent classes whose member functions share the same signature is not overriding
side note: you shouldn't include Daughter.hpp in Mother.cpp, whether you plan to inherit Mother in Daughter, or not.
Related
i have the following problem:
I have two classes:
FooClass.h
#include <vector>
#include "BaseClass.h"
using namespace std;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
And
BaseClass.h
#include "FooClass.h"
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
So, basically the function should be that i create one instance of FooClass.
Then i create multiple instances of BaseClass which i want to add to the vBaseClass vector in the FooClass.
If needet i want to access the BaseClass instance with vBaseClass[key] and call the doStuff() function in FooClass. I give FooClass as a pointer parameter because i want to have still access to the vBaseClass vector from the doStuff() function then which is called by an BaseClass instance.
Everything works fine, but when i add the vector i get the following errors:
error: ‘BaseClass’ was not declared in this scope
vector<BaseClass> vBaseClass;
error: template argument 1 is invalid
vector<BaseClass> vBaseClass;
error: template argument 2 is invalid
error: ‘BaseClass’ has not been declared
void addBaseClass(BaseClass &baseClass);
error: ‘FooClass’ has not been declared
void updateData(FooClass *pFooClass);
error: no matching function for call to ‘BaseClass::updateData(FooClass*)’
baseClass.updateData(this);
If someone knows the solution, many thanks!
Thanks to all, my issue is solved, here is the working code:
FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
#include <vector>
#include "BaseClass.h"
using namespace std;
class BaseClass;
class FooClass
{
public:
vector<BaseClass> vBaseClass;
void doStuff();
void addBaseClass(BaseClass &baseClass);
};
#endif //TESTPROJECT_FOOCLASS_H
FooClass.cpp
#include <iostream>
#include "FooClass.h"
void FooClass::doStuff()
{
cout << "Well nice done" << endl;
}
void FooClass::addBaseClass(BaseClass &baseClass)
{
baseClass.updateData(this);
vBaseClass.push_back(baseClass);
}
BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
#include "FooClass.h"
class FooClass;
class BaseClass {
public:
void updateData(FooClass *pFooClass);
};
#endif //TESTPROJECT_BASECLASS_H
BaseClass.cpp
#include "BaseClass.h"
void BaseClass::updateData(FooClass *pFooClass)
{
//We try to get some data, and if we get the data we call pFooClass->doStuff
pFooClass->doStuff();
}
main.cpp // console output "Well nice done"
#include "lib/FooClass.h"
int main()
{
FooClass fooclass;
BaseClass baseclass;
fooclass.addBaseClass(baseclass);
}
Thanks to everyone, it is my first activity and question here and i am really impressed how fast i got answers, really thanks!
#
Because i got asked to say what the issue was and how i solved it, i addet
to FooClass.h
#ifndef TESTPROJECT_FOOCLASS_H
#define TESTPROJECT_FOOCLASS_H
class BaseClass;
and to BaseClass.h
#ifndef TESTPROJECT_BASECLASS_H
#define TESTPROJECT_BASECLASS_H
class FooClass;
So the main issue was that i had to declare BaseClass and FooClass as shown above in the header of each others class.
Following this tutorial(https://www.youtube.com/watch?v=gq2Igdc-OSI&index=52&list=PLAE85DE8440AA6B83) I encountered 4 errors on Visual Studio C++ 2017. 3 of them are the same thing and just repeat 'Mother': base class undefined in the daughter.h file. The other error reads:'sayName' is not a member of 'Daughter' Now here is the code. It is quite simple what I want the program to print...I want it to print out two lines of "What are you doing there?" If you could help me with this answer, that would be great. Thank you.
For the Main file
`#include "stdafx.h"
#include
#include"Daughter.h"
#include"Mother.h"
using namespace std;
int main()
{
Mother pot;
pot.sayName();
Daughter kettle;
kettle.sayName();
int pause = 0;
cin >> pause;
}
Mother.h
#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
Mother();
void sayName();
};
#endif
Mother.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;
Mother::Mother()
{
}
void Mother::sayName() {
cout << "What are you doing there?" << endl;
}
Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter:public Mother
{
public:
Daughter();
};
#endif
Daughter.cpp
#include "stdafx.h"
#include<iostream>
#include"Daughter.h"
#include"Mother.h"
using namespace std;
Daughter::Daughter()
{
}
When a class inherits another, it must include the parent class header in its header. In your case, you must add #include "Mother.h" at the top of the daughter header (not only at the .cpp file). The other error is happening because of the first one and correcting it should solve it.
When you write the inheritance syntax class Daughter : public Mother, the Daughter class definition needs to have access to the information about its parent class for several reasons. One of them is the information about inherited methods, which was causing your second error.
I'm a first-year college student that doesn't know everything about CS yet, so please bear with my newness to it, and this is my first question on here.
For an assignment, we are making faux version of Pokemon Go to practice using polymorphism in c++, and I'm running into some compiler errors. Here are the three files with just a sample of the code in them:
#ifndef EVENT_H
#define EVENT_H
#include <string>
#include "Trainer.h"
class Event{
protected:
std::string title;
public:
Event();
~Event();
virtual void action(Trainer) = 0;
};
#endif
Trainer.h:
#ifndef TRAINER_H
#define TRAINER_H
#include "Pokemon.h"
class Trainer{
private:
Pokemon* pokemon;
int num_pokemon;
public:
Trainer();
~Trainer();
//include accessors and mutators for private variables
};
#endif
Pokemon.h:
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event{
protected:
std::string type;
std::string name;
public:
Pokemon();
~Pokemon();
virtual bool catch_pokemon() = 0;
};
#endif
The trainer.h file is a parent class for each pokemon type (eg Rock) which just defines a few virtual functions. The error I'm getting is when I'm compiling all of this and I get something that says:
Pokemon.h : 5:30: error: expected class-name befoer '{' token:
class Pokemon : Event {
Pokemon need to be a derived class to an event, so that an event pointer can point in another Location class can point to either a pokemon, pokestop, or cave for the assignment, and I have been looking online for hours and can't figure out what to do. I would appreciate the help! Let me know if you need more info or something because again, this is my first time posting a question.
You need some forward declarations.
In Event.h, you can put class Trainer; instead of #include "Trainer.h". In Trainer.h, you can put class Pokemon; instead of #include "Pokemon.h".
You will probably need to include the appropriate headers in the corresponding source files in order to actually use the other classes. But by avoiding the includes in the header files, you get out of the circular dependency trouble.
Pokemon.h must continue to #include "Event.h", since you're inheriting Event, which requires a complete definition.
Use forward declaration, to tell classes the type they need to use will be defined later. You can use forward declaration in situations where the size is know, pointers and references are always the same size regardless of the type they point to so use them.
#ifndef EVENT_H
#define EVENT_H
#include <string>
class Trainer;
class Event
{
protected:
std::string title;
public:
Event();
virtual ~Event();
virtual void action(Trainer* const trainer) = 0;
};
#endif
then
#ifndef TRAINER_H
#define TRAINER_H
class Pokemon;
class Trainer
{
private:
Pokemon* const pokemon;
int numPokemon;
public:
Trainer();
~Trainer();
};
#endif
then
#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event
{
protected:
std::string type;
std::string name;
public:
Pokemon();
virtual ~Pokemon();
virtual bool catchPokemon() = 0;
};
#endif
when using polymorphism (virtual functions) you must always make the base class destructor virtual too. It is also nice to make the derived classes destructor virtual as well, but it is not required.
I have a fairly simple situation, and the fact that I can't cope with it drives me crazy.
I have a class that is declared as follows:
// inc/Services/Specific/ReviewRetriever.h
#include "../../ReviewRetriever.h"
class Specific_ReviewRetriever : public ReviewRetriever
{
public:
Specific_ReviewRetriever(Service* service);
~Specific_ReviewRetriever() = default;
};
Implementation of the class goes as follows:
// src/Services/TrustedShops/ReviewRetriever.cpp
#include <string>
#include <vector>
#include "Service.h"
#include "Services/Specific/ReviewRetriever.h"
Specific_ReviewRetriever::Specific_ReviewRetriever(Service* service) :
ReviewRetriever(service)
{
}
std::string Specific_ReviewRetriever::prepare_update_link(std::string link)
{
}
std::vector<int> Specific_ReviewRetriever::parse_response(boost::property_tree::ptree responseXML)
{
}
This class inherits from the class that is declared as follows:
// inc/ReviewRetriever.h
#include <string>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include "Review.h"
class Service;
class ReviewRetriever
{
public:
~ReviewRetriever() = default;
void retrieve(std::vector<Review> & reviews);
protected:
ReviewRetriever(Service* service);
virtual std::string prepare_update_link(std::string link) = 0;
virtual std::vector<Review> parse_response(boost::property_tree::ptree responseXML) = 0;
Service* _service;
};
And this class on its part is defined as follows:
// src/ReviewRetriever.cpp
#include <vector>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include "Review.h"
#include "ReviewRetriever.h"
void ReviewRetriever::retrieve(std::vector<Review> & reviews)
{
}
So a fairly simple class and another one that inherits from it. But when I try to compile the code I get the following error:
no ‘std::string Specific_ReviewRetriever::prepare_update_link(std::string)’
member function declared in class ‘Specific_ReviewRetriever’
So, even though I got implementation of the class, the compiler doesn't seem to notice it (even though it sees that the class Specific_ReviewRetriever inherits from ReviewRetriever, it refuses to recognize its methods).
I build with cmake and here is the relevant part:
// src/CMakeLists.txt
file(GLOB_RECURSE sources *.cpp)
target_include_directories(my_target PRIVATE ${PROJECT_SOURCE_DIR}/inc/)
(here ${PROJECT_SOURCE_DIR} is src/../).
So as I mentioned, implementation and declaration of the classes is present, and yet compilation fails. I understand that the problem may not be on the surface, but I am really clueless where to start to track the error, maybe you have a piece of advice on that.
In case this excerpt of code is insufficient, entire code lies here.
Thank you in advance!
You still have to declare the functions that you're overriding in the derived class:
class Specific_ReviewRetriever : public ReviewRetriever
{
public:
Specific_ReviewRetriever(Service* service);
~Specific_ReviewRetriever() = default;
// Overrides:
virtual std::string prepare_update_link(std::string link);
virtual std::vector<int> Specific_ReviewRetriever::parse_response(boost::property_tree::ptree responseXML);
};
So to toy with friend functions, I decided to make a Child class and a Mother class. The Mother class has a data member that is a Child. The Child class makes two methods of the Mother class friend functions.
When I compile though, it seems that no matter how I handle inclusions I end up with an error. If the Child is the first one to get defined, I get "Mother is not a class or namespace name" for the line friend void Mother::setChildName(string name); in Child.h. If the Mother is the first one to get defined, I get "Missing type specifier" for the line Child c; in Mother.h.
Is there a way around this? I tried putting class Mother; at the top of the Child.h and class Child; at the top of Mother.h and that didn't seem to help.
Or is this kind of circular reference just always going to fail?
In Mother.h:
#ifndef MOTHER_H_
#define MOTHER_H_
#include <string>
#include "Child.h"
using namespace std;
class Mother {
public:
Mother();
void setChildName(string name);
string getChildName();
private:
Child c;
};
#endif
In Mother.cpp:
#include <string>
#include "Mother.h"
using namespace std;
void Mother::setChildName(string name) {
c.name = name;
}
string Mother::getChildName() {
return c.name;
}
In Child.h:
#ifndef CHILD_H_
#define CHILD_H_
#include <string>
#include "Mother.h"
using namespace std;
class Child {
public:
private:
string name;
friend void Mother::setChildName(string name);
friend string Mother::getChildName();
};
#endif
This particular problem cannot be solved without redesign. Mother needs to be defined before Child, and Child needs to be defined before Mother. The simple way is to make the whole Mother class a friend of Child. That way Child can be defined first.
I think there little practical benefit in making individual methods friends of another class. That would imply that your class is so big that it's responsibilities could be divided into smaller classes.
if Mother will hold pointer to child, you will not have to include child.h. forward deceleration will be enough.