How to access private variable from a parent class - c++

I have to do polymorphism for the go() function but i cant figure out how to access private variable distance. The complier keeps showing that
‘int Transport::distance’ is private within this context
5 | distance = 100;
Here is my code
Transport.h
#ifndef TRANSPORT_H
#define TRANSPORT_H
#include <iostream>
using namespace std;
class Transport{
private:
int distance;
public:
int get_dist_travelled();
virtual void go();
};
#endif
Transport.cpp
#include "Transport.h"
int Transport::get_dist_travelled(){
return distance;
}
Horse.h
#ifndef HORSE_H
#define HORSE_H
#include <iostream>
#include "Transport.h"
using namespace std;
class Horse:public Transport{
public:
void go();
};
#endif
Horse.cpp
#include "Horse.h"
#include "Transport.h"
void Horse::go() {
distance = 100;
}
main.cpp
#include <iostream>
#include "Transport.h"
#include "Horse.h"
using namespace std;
int main(){
Horse kid;
kid.go();
cout<<kid.get_dist_travelled()<<endl;
}

You can on the one hand make the variable protected, or define the class as a friend class.

Related

"identifier is undefined" when calling functions defined in class (separate cpp file) from main

I'm very new to c++ and to my understanding if i include the header file which is where the functions are defined, i should be able to call the functions from main? I have tried to add static or public before the functions but nothing seemed to change.
//heres my main
#include "pch.h"
#include "myfile1.h"
#include <iostream>
using namespace std;
int main(void){
function1();
function2();
}
//my myfile1 header
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
void function1();
void function2();
};
#endif
//my myfile1.ccp
#include "myfile1.h"
#include <iostream>
using namespace std;
class myClass{
void function1() {
}
void function2() {
}
}
Your functions are in the myClass in myfile1. You must create object from this class, and you can use functions where in the this class. My English skills are not good but you must do this:
int main(void){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
}
If you dont want create object from this class, you can do that :
myfile1.h:
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
void function1();
void function2();
#endif
myfile1.cpp:
#include "myfile1.h"
void function1(){
cout<<"function1"<<endl;
}
void function2(){
cout<<"function2"<<endl;
}
and main:
#include "myfile1.cpp"
int main(){
function1();
function2();
return 0;
}
If you want this functions with class, you can do it for good case:
myfile1.h
#ifndef myfile1_h
#define myfile1_h
#include <iostream>
using namespace std;
class myClass{
public: // If you want access these functions, you must use public tag.
// If you dont use any tag, it will be private, because default tag is private.
void function1();
void function2();
};
#endif
myfile1.cpp
#include "myfile1.h"
void myClass::function1(){
cout<<"function1";
}
void myClass::function2(){
cout<<"function2";
}
main:
#include "myfile1.h"
int main(){
myClass myClassObject;
myClassObject.function1();
myClassObject.function2();
return 0;
}
What is your compiler or editor? If you compile that codes from terminal, you must compile it like this:
g++ main.cpp myfile1.cpp
My English skills are not good. But I tried to explain.

How would I use aggregation with this class (beginner code)?

I would like to understand aggregation so I tried making a simple test code, but it doesn't work in my compiler, sometimes it works online.
code for main:
#include <iostream>
#include <string>
#include "player.h"
#include "game.h"
using namespace std;
int main(){
game game;
game.playGame();
return 0;
}
code for game.cpp:
#include "game.h"
#include "player.h"
game::game(){
player.setBalance(0);
}
void game::playGame(){
cout << "playing game." ; //just for debugging
}
code for game.h:
#ifndef GAME
#define GAME
#include <iostream>
using namespace std;
class game{
private:
player player;
public:
game();
void playGame();
};
#endif
code for player.cpp:
#include "player.h"
player::player(){
balance = 0;
}
player::player(int theBalance){
balance = theBalance;
}
int player::getBalance(){
return balance;
}
void player::setBalance(int theBalance){
balance = theBalance;
}
code for player.h:
#ifndef PLAYER // used on headers
#define PLAYER
class player{
private:
int balance;
public:
player();
player(int);
int getBalance();
void setBalance(int);
};
#endif
I think the problem is probably on the headers.
the error I get is:
In file included from main.cpp:5:0:
game.h:10:12: error: declaration of 'player game::player' [-fpermissive]
player player;
In file included from main.cpp:4:0:
player.h:5:7: error: changes meaning of 'player' from 'class player' [-fpermissive]
class player{
^~~~~~
I think that the problem is in class game, because it doesn't know a class player, use forward declaration or just write #include "player.h" in game.h

Access by "a pointer of the base class" to a method or attribute of the child class which was not declared in the base class(dynamically)

during one of our assignments in C++ programming ( Inheritance), I had to design an abstract class Shape with some attributes like color, rotate degree and etc, Which are common between shapes. However, during the implementation of Base classes like Circle, Rectangle and ... I had to add some attributes like Center of the circle (which it self-required its own setter and getter cause it's private ! ) or 4 corners of the rectangle (with its setter and getter ) which were not mentioned as a function(whether virtual or not) in out Base class.
Originally I wanted to have access to every method of derived class using a pointer of the base class.In my main, I used a Pointer of my base class, Shape * to have a Dynamic Bind to the common methods and attributes, but when it comes to setting the separate (uncommon between derived and base) attributes, it's not accessible via Pointer of the base class. I tried to declare them as virtual functions in my Base class, however, it doesn't work and it's also illogical, as the user might have many characteristics in one shape!
Does any one have any idea how can this problem be solved?
and how can I have access to those mentioned methods and attributes that are only declared in the Derived class, using Shape*?
tnx
this is my base class for shapes.
class Shape
{
public:
virtual void set_color(int color)=0;
virtual void set_border_color(int border_color)=0;
virtual void set_degree(float border_width)=0;
virtual void set_border_width(double rotate_degree)=0;
virtual void set_opacity(double opacity)=0;
protected:
int color;
int border_color;
float border_width;
double rotate_degree;
double opacity;
};
respectively are my Circle and Rectangle class
circle Header:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "shape.h"
class Circle :public Shape
{
public:
void set_color(int _color);
void set_border_color(int _border_color);
void set_degree(float rotate_degree);
void set_border_width(double border_width);
void set_opacity(double _opacity);
virtual void set_x_center(int _x_center);
virtual void set_y_center(int _y_center);
virtual void set_radius(int _radius);
virtual void set_name(std ::string _name);
int get_color();
int get_x_center();
int get_y_center();
std::string get_name();
private:
int x_center;
int y_center;
int radius;
std ::string name;
};
#endif // CIRCLE_H
circle CPP:
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include "circle.h"
#include "shape.h"
void Circle::set_color(int _color){ color=_color;}
void Circle::set_border_color(int _border_color){border_color=_border_color;}
void Circle::set_degree(float _rotate_degree){rotate_degree=_rotate_degree;}
void Circle::set_border_width(double _border_width){border_width=_border_width; }
void Circle::set_opacity(double _opacity){opacity=_opacity;}
int Circle::get_color(){return color;}
void Circle::set_x_center(int _x_center){ x_center=_x_center;}
void Circle::set_y_center(int _y_center){ y_center=_y_center;}
void Circle::set_radius(int _radius){ radius=_radius;}
void Circle::set_name(std ::string _name){ name=_name;}
int Circle::get_x_center(){return x_center;}
int Circle::get_y_center(){return y_center;}
std::string Circle::get_name(){return name;}
rectangle HEADER:
#ifndef RECT_H
#define RECT_H
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include "rect.h"
#include "shape.h"
class Rect : public Shape
{
public:
void set_color(int _color);
void set_border_color(int _border_color);
void set_degree(float _border_width);
void set_border_width(double _rotate_degree);
void set_opacity(double _opacity);
void set_first_point(int _first_x,int _first_y);
void set_second_point(int _second_x,int _second_y);
void set_name(std ::string _name);
private:
int first_point [2];
int second_point [2];
std ::string name;
};
#endif // RECT_H
rectangle CPP:
#include "rect.h"
#include "shape.h"
void Rect::set_color(int _color){ color=_color;}
void Rect::set_border_color(int _border_color){border_color=_border_color;}
void Rect::set_degree(float _border_width){border_width=_border_width;}
void Rect::set_border_width(double _rotate_degree){rotate_degree=_rotate_degree;}
void Rect::set_opacity(double _opacity){opacity=_opacity;}
void Rect::set_first_point(int _first_x,int _first_y){first_point[0]=_first_x;first_point[1]=_first_y;}
void Rect::set_second_point(int _second_x,int _second_y){second_point[0]=_second_x;second_point[1]=_second_x;}
void Rect::set_name(std ::string _name){name=_name;}
and here is my main
#include <cstdlib>
#include <vector>
#include <cmath>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include "shape.h"
#include "circle.h"
using namespace std;
int main()
{
Circle a;
Shape* b;
b=&a;
b->set_color(12);
b->set_x_center(30);
cout<< b->get_x_center();
return 0 ;
}
Originally I wanted to have access to every method of derived class using a pointer of the base class.
You can do so by dynamic casting back to the Circle *:
b->set_color(12);
dynamic_cast<Circle *>(b)->set_x_center(30);
std::cout << dynamic_cast<Circle *>(b)->get_x_center();
This works already, however, when dynamic casting like this make sure the result is not a nullptr
b->set_color(12);
Circle *c = dynamic_cast<Circle *>(b);
if (c != nullptr)
{
c->set_x_center(30);
std::cout << c->get_x_center();
}

Arduino: Inheritance and arrays of pointer subclasses

This is problem #2 from this previous question:
Inheritance in Arduino Code
Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.
This is my "Board" class I have so far, that contains multiple child elements:
Board.h:
#ifndef Board_h
#define Board_h
#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>
#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"
class Board
{
public:
Board();
void draw(double* matrix);
private:
Marble marble;
//std::vector<Actor> children;
Actor* children[2];
};
#endif
Board.cpp:
#include "Arduino.h"
#include "Board.h"
#include <math.h>
#include <iterator>
#include <vector>
Board::Board()
{
}
void Board::create(double* _matrix, int _cols, int _rows) {
Marble *marble = new Marble();
Wall wall;
children[0] = marble;
//children.push_back(marble);
//children.push_back(wall);
}
void Board::draw(double* matrix) {
Serial.println("board draw");
children[0]->speak();
}
In my "loop" function I am calling
board.draw(matrix);
which results in some nutty Serial code being written out.
Clearly I am not understanding the ins and outs of pointers in arrays in classes here.
You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.
class Actor
{
public:
Actor();
virtual void speak(); // virtual
private:
};

unknown type error in C++

What is going on?
#include "MyClass.h"
class MyOtherClass {
public:
MyOtherClass();
~MyOtherClass();
MyClass myVar; //Unknown type Error
};
Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.
How can it be unknown when the .h is included right there?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
#pragma once
#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"
#include "Node.h"
#include "CursorMano.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;
typedef boost::shared_ptr<class NodeButton> NodeButtonRef;
class NodeButton : public Node2D
{
public:
NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
virtual ~NodeButton ();
//methods
void update( double elapsed );
void draw();
void setup();
//events
bool mouseMove( ci::app::MouseEvent event );
//vars
CursorMano *mCursor;
gl::Texture mImageTexture;
Anim<float> mAlpha = 1.0f;
bool mSelected = false;
private:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
#pragma once
#include <list>
#include <vector>
#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "NodeButton.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CursorMano {
public:
CursorMano (AppBasic *app);
~CursorMano ();
void mueveMano(Vec2i);
void update();
void draw();
void play(int button);
void reset(int button);
Vec2i mMousePos;
NodeButton mButtonCaller; //this gives the unknow type error
private:
AppBasic *mApp;
gl::Texture mFrameTexture;
qtime::MovieGl mMovie;
int mIdButton;
};
You have a circular dependency of your header files.
NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.
You will need to use forward declarations to break this circular dependency.
In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.
using namespace std;
using namespace is;
class CursorMano;
It's probably a result of the circular dependency between you two header files (NodeButton includes CursorMano and CursorMano includes NodeButton). Try removing the #include "CursorMano.h" in NodeButton.h and add class CursorMano; before your NodeButton declaration.