I am new to c++ and is trying to learn about its Object-Oriented design. I started a small project to test out inheritance and polymorphism but encountered a problem and can't seen to figure out what went wrong.
Whenever I compile, there will be an error "class 'ShapeTwoD' has no member name getx() and gety()". I tried to directly set the x and y value with setx and sety but it still return the same error.
Class ShapeTwoD is the Base class with only the variable 'name' and 'container'. Would appreciate if anyone can direct me to the right direction.
Main.cpp
#include <iostream>
#include <string>
#include "ShapeTwoD.h"
#include "Square.h"
using namespace std;
int main()
{
cout<<endl;
ShapeTwoD *shape2D[100];
ShapeTwoD *sq1 = new Square("Square", true, 4, 6);
cout << sq1->getName() <<endl;
cout << sq1->getContainer() <<endl;
//sq1->setx(4) <<endl;
//sq1->sety(6) <<endl;
cout << sq1->getx() <<endl;
cout << sq1->gety() <<endl;
cout<<endl;
delete sq1;
}
Square.h
#include <iostream>
#include <string>
#include "ShapeTwoD.h"
using namespace std;
class ShapeTwoD; //forward declare
class Square : public ShapeTwoD
{
public:
int x;
int y;
//constructor
Square(string name, bool container,int x, int y);
int getx();
int gety();
void setx(int x);
void sety(int y);
};
Square.cpp
#include <iostream>
#include <string>
#include "Square.h"
#include "ShapeTwoD.h"
Square::Square(string name, bool containsWarpSpace, int coordx, int coordy)
:ShapeTwoD(name, containsWarpSpace)
{
(*this).x = coordx;
(*this).y = coordy;
}
int Square::getx()
{
return (*this).x;
}
int Square::gety()
{
return (*this).y;
}
void Square::setx(int value)
{
(*this).x = value;
}
void Square::sety(int value)
{
(*this).y = value;
}
That's normal... If you declare sq1 as ShapeTwoD, you have access to ShapeTwoD public member methods/attributes. Even it was instanciated with the Square constructor. Cast it as Square, and you can use getx gety. Or declare getx/gety as methods of ShapeTwoD.
Well this is what you should expect since it has shape2D type , Although constructing it with the square constructor won't allow you to access the derived class members but it will allow you to have a safe type cast to use it.. the simplest way to do it is by :
cout << static_cast<Square*>(sq1)->getx() << endl;
cout << static_cast<Square*>(sq1)->gety() << endl;
Related
I'm having this issue on another program, but I tried to simplify it with this one. I cannot set the weapon name through p.getWeaopn().setName("sword"); It works fine when I simply set it through its own object, but when I try to access the setter through player it doesn't set anything.
#include <iostream>
#include <string>
#include "Player.h"
#include "Weapon.h"
using namespace std;
int main()
{
Player p; // Player contains only a Weapon weapon;
Weapon w; // Weapon only contains a string name;
//w.setName("sword"); // this changes the name of the weapon
p.setWeapon(w);
p.weapon.setName("sword"); // this also changes the name
p.getWeapon().setName("sword"); // this is not setting the name. Why?
// checking if weapon has a name
if (p.getWeapon().getName().empty())
{
cout << "Weapon name is empty!" << endl;
}
else
{
cout << "Weapon name is " << p.getWeapon().getName() << endl;
}
}
Weapon.h
#pragma once
#include <string>
using namespace std;
class Weapon
{
private:
string name;
public:
string getName();
void setName(string);
};
Weapon.cpp
#include "Weapon.h"
string Weapon::getName()
{
return name;
}
void Weapon::setName(string n)
{
name = n;
}
Player.h
#pragma once
#include "Weapon.h"
class Player
{
private:
Weapon weapon;
public:
Weapon getWeapon();
void setWeapon(Weapon);
};
Player.cpp
#include "Player.h"
Weapon Player::getWeapon()
{
return weapon;
}
void Player::setWeapon(Weapon w)
{
weapon = w;
}
Weapon Player::getWeapon()
You return a copy and not a reference of the weapon, so any change to the copy does not affect the original.
For return a reference, use & operator:
Weapon& Player::getWeapon()
{
return this->weapon;
}
Player::getWeapon() returns a copy of the weapon every time instead of a reference to the weapon. Changing the name in the copy changes nothing in the original.
I started to pick up some c++ programming and i am learning the concept of classes and objects. So I looked on the web for some exercises I can practice on. I have read that it is good practice to have the main, header and constructor file separated and not into one long file.
I'm trying to break the following code into three separate files :
// Exercises: Classes
// Exercise 3
// Exercises: Classes
// Exercise 3
#include <iostream>
using namespace std;
class Student{
public:
char *name;
int mark1;int mark2;
Student(char* na, int ma1,int ma2){
name=na;mark1=ma1;mark2=ma2;
}
int calc_media(){return (mark1+mark2)/2;}
void disp(){
cout << "Student:" << name << " \n media:"<< calc_media() <<"\n";
}
};
int main(){
char* nam;int m1,m2;
cout << "Enter name:";
cin>> nam;
cout << "Enter marks of two subjects:";
cin>> m1;
cin>> m2;
Student student1(nam,m1,m2);
student1.disp();
return 0;
}
into the following files:
main.cpp:
#include <iostream>
#include <string>
#include "student_example.h"
using namespace std;
int main()
{
int marc1,marc2;
char nam;
cout<<"Please enter the name of the student: ";
cin>>nam;
cout<<"Please enter the two grades of the student"<<"\n grade one:";
cin>>marc1;
cout<<"Grade two";
cin>>marc2;
student_Example student1;
student1.disp();
return 0;
}
header file (student_Example.h)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
And constructor:
#include <iostream>
#include <string>
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2)
{
name=nam;
mark1=marc1;
mark2=maec2;
}
Im getting the error
"error: prototype for **'student_Example::student_Example(char, int, int)' does not match any class 'student_Example'**
Any advice what may be going on here? Thanks in advance :)
Your header file with class student_Example doesn't promise a constructor. (And seems to be missing and #endif)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
#endif //<-- this too
While we are there we can use an member initialiser list in the constructor
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
Edit:
Note that student_Example(char nam, int marc1, int marc2) is a declaration that you will define a constructor taking a char and two ints., which you have done in your cpp file.
You can make an object like this
student_Example example('n', 1, 2);
Without this non-default constructor, a default constructor taking no parameters would have been automatically generator for you, so you could have made an object like this:
student_Example example;
Now you have defined a constructor that will no longer happen. You either need to add this to your class, or make sure you use the constructor taking parameters.
doctorlove fixes your issue, but is also good practice to have the methods in the .cpp file, like this:
student_Example.h
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media();
void disp();
};
#endif
student_Example.cpp
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
int student_Example::calc_media(){
return (mark1+mark2/2);
}
void student_Example::disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
Im trying to seperate a class and its superclass into two different header and cpp files. In the main Method I want to include both of them.
Currently my main programm example.cpp looks like this:
#include <iostream>
#include "header.h"
using namespace std;
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
My Rectangle.cpp like this:
#include <iostream>
#include "Shape.h"
#include "Rectangle.h"
using namespace std;
int getArea()
{
return (Shape::width * Shape::height);
}
And my Rectangle.h:
class Rectangle: public Shape
{
public:
int getArea();
};
The Shape.cpp:
#include <iostream>
#include "Shape.h"
using namespace std;
void Shape::setWidth(int w)
{
width = w;
}
void Shape::setHeight(int h)
{
height = h;
}
And the Shape.h:
class Shape
{
public:
void setWidth(int w);
void setHeight(int h);
int width;
int height;
};
The header.h just includes the two headers of the classes:
#include "Shape.h"
#include "Rectangle.h"
If I compile it, the compiler says:
Lukass-MacBook-Pro:Oberklassenbeispiel Lukas$ g++ -c Rectangle.cpp
Rectangle.cpp:9:17: error: invalid use of non-static data member 'width'
return (Shape::width * Shape::height);
~~~~~~~^~~~~
Rectangle.cpp:9:32: error: invalid use of non-static data member 'height'
return (Shape::width * Shape::height);
~~~~~~~^~~~~~
2 errors generated.
It seems, that the Rectanlge.cpp cant see the attributes of the superclass. How do I fix this?
When you #include, you provide the file name, not the classes specified in the file. It is quite common to have multiple closely coupled classes in a single file. Your example isn't really the best (it is single inheritance, not multiple as the title suggests), but working with that...
You could have a file, shapes.h with the following contents :
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
Then in your main function (and the file containing it).
#include <iostream>
#include "shapes.h"
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.
#include "player.h"
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
player a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
vector <player> a;
player a;
}
team::addPlayer(player)
{
a.push_back(a);
}
If more info is needed please ask. Thank you in advance for any help.
I assume this is what you meant:
#include "player.h"
#include <vector>
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
vector<player> a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
}
team::addPlayer(player p)
{
a.push_back(p);
}
You shoud have your vector variable a member of your class or create it on heap a keep the pointer to it. Now you are creating your vector on a stack in teem constructor. It will deleted when constructor is finished.
Also you can't use name a for both player and vector. I recommend you to read some C++ books first.
There's so much wrong with this I don't know where to begin, you declare a SINGLE player, here:
private:
player a; // why not just call every variable in your program "a"?
And then in your constructor of the team:
team::team()
{
vector<player> a; // a vector that will be destroyed on exit from constructor
player a; // another single player, but you've just defined 'a' so you should get a compiler error along the lines of redefinition.
}
I suspect you want something like:
#include <vector>
#include <string>
#include "player.h"
class team
{
private:
std::vector<player> m_Players; // players
public:
void addPlayer(const player& _player) { m_Players.push_back(_player); }
}; // eo class team
What do u need ? Store players in your class team ?
#include <iostream>
#include <vector>
using namespace std;
class Player
{
public:
Player(int id) { m_id = id; }
int GetId() {return m_id; }
private:
int m_id;
};
class team
{
public:
team(){};
void AddPlayer(Player p) {m_arr.push_back(p);}
size_t Size(){ return m_arr.size();}
Player GetPlayer(size_t index) {return m_arr[index];}
private:
vector<Player> m_arr;
};
void main()
{
team t;
for (int i =0; i < 10; ++i)
{
t.AddPlayer(Player(i));
}
for (int i =0; i < t.Size();++i)
{
cout << "Player [" << i + 1 << "] with id: " << t.GetPlayer(i).GetId() << endl;
}
}