Why is the Default Parameter being used each time? Initializing classes - c++

When I printed the variables passed through, the default is printed first, followed by what I want passed. So the final result remains the same. The initialization is found in Owner.h and Owner.cpp. Variables are passed starting from the Dog.cpp. I've also tried changing my print statements to Dog.owner... but the result was the same.
Owner.h
#define OWNER_H
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
class Owner {
private:
string name;
int age;
public:
Owner(string ownerName = "Lucy" , int ownerAge = 10); // default params
string getName();
int getAge();
};
#endif
Owner.cpp
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
// Getters
string Owner::getName() {return name;}
int Owner::getAge() {return age;}
// Constructors
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
Owner::getName();
Owner::getAge();
}
Dog.h
#ifndef DOG_H
#define DOG_H
#include <iostream>
#include <string>
#include "Owner.h"
using namespace std;
class Dog {
private:
string breed;
int age;
Owner owner;
static int dogCount;
public:
Dog();
Dog(string, int);
// Getter and Setter methods
void setBreed(string var);
void setAge(int var);
string getBreed();
int getAge();
// Other
void printDogInfo();
static int getDogCount() {return dogCount;}
};
#endif
Dog.cpp
#include <iostream>
#include <string>
#include "Dog.h"
#include "Owner.h"
using namespace std;
// Constructors
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
Dog::Dog() {
}
void Dog::printDogInfo() {
cout << "owner: " << owner.getName() << ", " << owner.getAge() << " yo" << endl << endl;
}
int main() {
Dog myDog1("Belle", 15);
myDog1.setBreed("Siberian Husky");
myDog1.setAge(2);
myDog1.printDogInfo();
return 0;
}

Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
By:
Dog::Dog(string ownerName, int ownerAge) : Owner(ownerName, ownerAge) {
dogCount++;
}
Probably, you also want to fix this:
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
// Owner::getName(); not needed
// Owner::getAge(); not needed
}

Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
is equivalent to
Dog::Dog(string ownerName, int ownerAge) :
breed(),
owner()
{
Owner(ownerName, ownerAge); // Create temporary
dogCount++;
}
You probably want instead:
Dog::Dog(string ownerName, int ownerAge) :
breed(),
age(0),
owner(ownerName, ownerAge)
{
dogCount++;
}

Related

No variable member function declared in class?

can sombody explain to me why my code will not work, and how to fix it thanks :)
I keep recieving this error :
no 'int burrito::setName()' member function declared in class 'burrito'
My goal is to call a function from a different class file
My main.cpp :
#include <iostream>
#include "burrito.h"
using namespace std;
int main()
{
burrito a;
a.setName("Ammar T.");
return 0;
}
My class header (burrito.h)
#ifndef BURRITO_H
#define BURRITO_H
class burrito
{
public:
burrito();
};
#endif // BURRITO_H
My class file (burrito.cpp):
#include "burrito.h"
#include <iostream>
using namespace std;
burrito::setName()
{
public:
void setName(string x){
name = x;
};
burrito::getName(){
string getName(){
return name;
};
}
burrito::variables(string name){
string name;
};
private:
string name;
};
Your code is a mess. You need to write function prototypes in the header file and function definitions in the cpp file. You are missing some basic coding structures. See below and learn this pattern of coding:
This code should work and enjoy burritos !
main():
#include <iostream>
#include "Header.h"
int main()
{
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
getchar();
return 0;
}
CPP file:
#include "Header.h"
#include <string>
void burrito::setName(std::string x) { this->name = x; }
std::string burrito::getName() { return this->name; }
Header file:
#include <string>
class burrito
{
private:
std::string name;
public:
void setName(std::string);
std::string getName();
//variables(string name) {string name;} // What do you mean by this??
};
Your poor little burrito is confused. Confused burritos can't help much.
You may want your burrito declaration as:
class Burrito
{
public:
Burrito();
void set_name(const std::string& new_name);
std::string get_name() const;
private:
std::string name;
};
The methods could be defined in the source file as:
void
Burrito::set_name(const std::string& new_name)
{
name = new_name;
}
std::string
Burrito::get_name() const
{
return name;
}
The header file only has a constructor for the class. The member functions
setName(string) and getName()
are not declared in the header file and that is why you get the error.
Also, you need to specify the return type for functions.
One way to do this would be
//Header
//burrito.h
class burrito{
private:
string burrito_name;
public:
burrito();
string getName();
void setName(string);
}
//burrito.cpp
#include "burrito.h"
#include <iostream>
using namespace std;
string burrito::getName()
{
return burrito_name;
}
void burrito::setName(string bname)
{
bname =burrito_name;
}
This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:
#include <iostream>
#include <string>
using namespace std;
class burrito {
public:
void setName(string s);
string getName();
private:
string name;
};
void burrito::setName(string s) {
name = s;
}
string burrito::getName() {
return name;
}
int main() {
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
return 0;
}

C++ composition circular dependency

I'm trying to learn C++ and currently I'm trying to know how to implement an object composition in this language.
I have a Character class which is inherited by a Hero and a Monster class.
A Character has a NormalAbility and a SpecialAbility.
I've made the NormalAbility and SpecialAbility classes and both are inheriting an Ability superclass.
My problem is that when I put the #include "Character.h" in Ability.h the normalAbility and specialAbility variables in Character.h don't get recognized as their respected classes. Errors such as "syntax error : identifier string" shows in the headers of both Ability inherited classes
Here's my code:
Character.h
#pragma once
#include <string>
#include "NormalAbility.h"
#include "SpecialAbility.h"
using namespace std;
class Character
{
public:
Character(string name, string type, int hp, NormalAbility na,
SpecialAbility sa);
bool isDead();
void damage(int amt);
void heal(int amt);
void attack(Character* c, int amt);
private:
string name;
string type;
int hp;
int maxHp;
NormalAbility* normalAblity;
SpecialAbility* specialAbility;
}
Character.cpp
#include "Character.h"
#include <iostream>
Character::Character(string name, string type, int hp, NormalAbility* na,
SpecialAbility* sa)
{
this->name = name;
this->type = type;
this->maxHp = hp;
this->hp = hp;
normalAbility = na;
specialAbility = sa;
}
bool Character::isDead(){
return hp <= 0;
}
void Character::damage(int amt){
if (hp > 0){
hp -= amt;
}
else{
hp = 0;
}
}
void Character::heal(int amt){
if (hp + amt > maxHp){
hp = maxHp;
}
else{
hp += amt;
}
}
void Character::attack(Character* c, int amt){
c->damage(amt);
}
Hero.h
#pragma once
#include "Character.h"
#include <string>
using namespace std;
class Hero :
public Character
{
public:
Hero(string name, int hp);
}
Hero.cpp
#include "Hero.h"
#include <iostream>
Hero::Hero(string name, int hp)
: Character(name, "Hero", hp)
{
}
Ability.h
#pragma once
#include <string>
#include "Character.h"
using namespace std;
class Ability
{
public:
Ability(string name, string type, Character* owner);
void levelUp();
private:
string name;
string type;
int level;
Character* owner;
}
Ability.cpp
#include "Ability.h"
Ability::Ability(string name, string type, Character* owner)
{
this->name = name;
this->type = type;
this->owner = owner;
level = 1;
}
void Ability::levelUp(){
level++;
}
NormalAbility.h
#pragma once
#include "Ability.h"
#include <string>
using namespace std;
class NormalAbility :
public Ability
{
public:
NormalAbility(string name);
}
NormalAbility.cpp
#pragma once
#include "NormalAbility.h"
#include <string>
using namespace std;
NormalAbility::NormalAbility(string name) : Ability(name, "Normal")
{
//some codes
}
This way you avoid the circular include of the .h files, because you're including it in the .cpp, and in the .h you're saying that, "Character exists but don't care about his definition right now"
Ability.h
#pragma once
#include <string>
class Character;
using namespace std;
class Ability
{
public:
Ability(string name, string type, Character* owner);
void levelUp();
private:
string name;
string type;
int level;
Character* owner;
}
Ability.cpp
#include "Ability.h"
#include "Character.h"
Ability::Ability(string name, string type, Character* owner)
{
this->name = name;
this->type = type;
this->owner = owner;
level = 1;
}
void Ability::levelUp(){
level++;
}

C++ class and inheritance error: undefined reference to derived class

I am attempting to make part of a program that uses a bank account class as the base class and checking and savings as the derived classes. I have been trying to set up the basic framework before I do any fancy data handling and I've followed some tutorials to get a better understanding of classes and inheritance.
I have looked for answers but the answers I have found don't seem to be my problem but I might just need another set of eyes on my code.
the compiler errors:
In function main':
badriver.cpp:20: undefined reference toChecking::getAccount()'
badriver.cpp:23: undefined reference to Checking::setAccount(int)'
badriver.cpp:24: undefined reference toSavings::setAccount(int)'
badriver.cpp:26: undefined reference to `Checking::getAccount()'
badriver.cpp
#include "BankAccount.cpp"
#include "Checking.cpp"
#include "Savings.cpp"
#include <string>
#include <iostream>
using namespace std;
int main(){
Checking c;
Savings s;
cout << "Checking: " << c.getAccount() << " - Type: " << c.getType() << endl;
cout << "Savings: " << s.getAccount() << " - Type: " << s.getType() << endl;
c.setAccount(9);
s.setAccount(15);
cout << "New Checking: " << c.getAccount() << endl;
cout << "New Savings: " << s.getAccount() << endl;
return 0;
}
BankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;
using std::ostream;
using std::istream;
class BankAccount{
private:
int myAccount;
const char* color;
public:
// default constructor
BankAccount();
BankAccount(int account);
virtual ~BankAccount();
virtual void setAccount(int)=0;
int getAccount();
//
// void setSAccount(int);
// int getSAccount();
//
virtual const char* getColor();
virtual const char* getType() = 0;
//virtual const char* getCType() = 0;
protected:
void setColor(const char*);
};
#endif // BANKACCOUNT_H
BankAccount.cpp
#include "BankAccount.h"
#include "Checking.h"
#include "Savings.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
// default constructor
BankAccount::BankAccount(){
account = 1;
}
BankAccount::~BankAccount(){}
// void BankAccount::setAccount(int account){
// myAccount = account;
// }
int BankAccount::getAccount(){
return myAccount ;
}
BankAccount::BankAccount(int account){
myAccount = account;
}
const char* BankAccount::getColor(){
return color;
}
void BankAccount::setColor(const char* c){
color = c;
}
Checking.h
#ifndef CHECKING_H
#define CHECKING_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Checking : public BankAccount{
private:
const char* type;
public:
Checking();
virtual ~Checking();
void setAccount(int account);
virtual const char* getType();
void setChecking(int);
int getChecking();
};
#endif //CHECKING_H
Checking.cpp
#include "Checking.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Checking::Checking() : BankAccount(1), type("Checking"){}
Checking::~Checking(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Checking::getType(){
return type;
}
Savings.h
#ifndef SAVINGS_H
#define SAVINGS_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Savings: public BankAccount{
private:
const char* type;
public:
Savings();
virtual ~Savings();
void setAccount(int account);
virtual const char* getType();
void setSavings(int);
int getSavings();
};
#endif // SAVINGS_H
Savings.cpp
#include "Savings.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Savings::Savings() : BankAccount(2), type("Savings"){}
Savings::~Savings(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Savings::getType(){
return type;
}
Thanks for any help pointing me in the right direction.
Checking.cpp and Savings.cpp contain:
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
This causes undefined behaviour because you defined those functions in multiple files. You need to delete those lines from Checking.cpp and Savings.cpp, and instead put in definitions for the functions which are listed as being missing in the compiler output:
void Checking::setAccount(int account){
// code here
}
etc.

How exactly serialization works in C++?

Here are my code files:
main.cpp
#include <iostream>
#include <fstream>
#include "Car.h"
#include "Engine.h"
using namespace std;
int main() {
Car* car = new Car(1984);
/* do something here */
delete car;
return 0;
}
Car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;
#include "Engine.h"
class Car {
public:
Car(int);
virtual ~Car();
void serialize(ostream& s) {
engine.serialize(s);
s << ' ' << yearModel;
}
void unserialize(istream& s) {
engine.unserialize(s);
s >> yearModel;
}
private:
Engine engine;
int yearModel;
};
#endif /* CAR_H */
Car.cpp
#include "Car.h"
Car::Car(int year) {
yearModel = year;
}
Car::~Car() {
}
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include <iostream>
using namespace std;
class Engine {
public:
Engine();
virtual ~Engine();
void serialize(ostream& s) {
s << ' ' << engineType;
}
void unserialize(istream& s) {
s >> engineType;
}
private:
int engineType;
};
#endif /* ENGINE_H */
Engine.cpp
#include "Engine.h"
Engine::Engine() {
engineType = 1;
}
Engine::~Engine() {
}
What I want to do in the main.cpp is to save the created Car object to file.txt and later read it from there. How does that exactly work? For example: how do I call the serialization function in Car class?
I'm sorry if I sound like a noob, but this whole serialization thing is pretty new to me.
Edit: Code compiles now when I added 'void' in front of all serialize- and unserialize-functions.
This has nothing to do with serialization: a function needs a return type, even if it is void. So this is wrong:
serialize(ostream& s) // look, no return type.
You probably need to either return void,
void serialize(ostream& s) { /* code as before */ }
or return the stream by reference to allow for chaining:
ostream& serialize(ostream& s) {
return s << ' ' << engineType;
}

Passing string as reference to function pointer c++

Below is my code
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
using namespace std;
class ToDoCommands
{
public:
void getCommand(string);
};
void ToDoCommands::getCommand(string command)
{
cout<<command; //here i get ping
void (*CommandToCall)(void);
CommandToCall = command; // error here i want something like
// CommandToCall = ping
CommandToCall();
}
void ping(void)
{
cout<<"ping command executed";
}
int main()
{
ToDoCommands obj;
obj.getCommand("ping");
}
The function pointer should refer to function ping dynamically. A string same as function name is passed to getCommand function in main.
C++ just doesn't work that way. If you really need something like that, you'll have to make a table of functions that are indexed by name:
#include <assert.h>
#include <iostream>
#include <map>
#include <string>
using std::cout;
using std::string;
using std::map;
void ping(void)
{
cout << "ping command executed\n";
}
class ToDoCommands
{
public:
typedef void (*FunctionPtr)();
typedef string Name;
void registerFunction(Name name,FunctionPtr);
void callFunction(Name);
private:
map<Name,FunctionPtr> func_map;
};
void ToDoCommands::registerFunction(Name name,FunctionPtr func_ptr)
{
func_map[name] = func_ptr;
}
void ToDoCommands::callFunction(Name name)
{
assert(func_map.find(name)!=func_map.end());
func_map[name]();
}
int main(int argc,char **argv)
{
ToDoCommands to_do_commands;
to_do_commands.registerFunction("ping",ping);
to_do_commands.callFunction("ping");
return 0;
}
void ping(void)
{
// LL DD…DD XX
cout<<"ping command executed"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)(void)); //getCommand(ping)
};
void ToDoCommands::getCommand( void (*CommandToCall)(void) )
{
void (*CommandToCall1)(void);
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
ToDoCommands obj;
obj.getCommand( ping );
return 0;
}
i tried this and its working :)