|
Can a child class pointer access the base class members ?
|
|
|
|
|
It can access public members of the base class.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
so this would be by typecasting the pointer as the base class object pointer or direct access ?
|
|
|
|
|
No need of typecasting.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
|
I'm not sure I fully understood the question but if he's talking about a child class accessing the members of the base class (I didn't get the part with the pointer...), then it can also access protected members.
Of course, this is in the case of public inheritance (is there actually anybody using another kind of inheritance ?)
|
|
|
|
|
This is what I think his question is -
class Base
{
protected:
int i;
};
class Child : public Base
{
};
void main()
{
Child* ptr = new Child;
ptr->i;
}
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
|
Will stop posting this question over and over again ? If you didn't get any reply yet it can mean that nobody knows the answer or your question is so vague that nobody can reply.
Yesterday somebody asked for clarifications and you didn't reply. Somebody else even gave you a solution. So what's the problem with it ? You don't care because it's not a full code snippet that works out of the box ?
|
|
|
|
|
|
|
|
Purish Dwivedi wrote: Some more clarification is needed...
So why didn't you ask him instead of reposting your question as if nobody replied ?
|
|
|
|
|
How to write a capture video data to .avi or .wmv file
|
|
|
|
|
I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
float getX();
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
}
Point::~Point()
{
cout << "Destructing now!!! " << (int)this << endl;
}
float Point::getX()
{
return x;
}
int main()
{
Point myLocation(23, 54, 32);
cout << myLocation.getX() << endl;
return 0;
}
|
|
|
|
|
You are trying to get float variable X which has no assigned value.
Point::Point(float f_x, float f_y, float f_z)
{
// Assign value to your variable
x = f_x; y = f_y; z = f_z;
cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
}
|
|
|
|
|
When I instantiate the object with this:
Point myLocation(23, 54, 32);
That should assign values 23, 54, 32 to x, y, and z, respectively.
|
|
|
|
|
No. you are wrong here. If you debug your code you can see the wrong.
|
|
|
|
|
I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
|
|
|
|
|
Michael Randolph wrote: I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
I think you are seriously mistaken here: the compiler is no magic. You declare a constructor and pass two variables in that constructor. If you don't do anything with those values, the compiler won't magically assign them to your class members. That would be crazy...
You have to assign them yourself as the other poster already told you.
Michael Randolph wrote: I thought once you instantiate the object it sets the variables that I have for parameters in the function?
Once again, the compiler is no magic, it can't guess what you want to do with the variables. What if you pass less variables than you have class members (or more) ? What if they are in different order ? In most of the cases, a class is much more than two member variables (sometimes around 100 members) and in general you pass only a few of them in the constructor. So, how will the compiler know tho which member variables he has to assign those ? That would be crazy...
|
|
|
|
|
Calling a constructor is no different from calling a normal function in this sense. The arguments supplied needs to be handled explicitly.
|
|
|
|
|
Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:
Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
}<pre>
or explicit assignments
<pre>Point::Point(float f_x, float f_y, float f_z)
{
x = f_x;
y = f_y;
z = f_z;
cout << "We're in the constructor with arguments " << (int)this << endl;
}
As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:
g++ -o inheritance inheritance.cpp
inheritance.cpp: In function ‘int main()’:
inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)
here is my source:
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
void getXYZ(float &X, float &Y, float &Z);
float getX();
float getY();
float getZ();
void setXYZ(float X, float Y, float Z);
void setX(float X);
void setY(float Y);
void setZ(float Z);
Point operator = (Point &p);
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
x = f_x;
y = f_y;
z = f_z;
}
Point::~Point()
{
cout << "Destructing now!!! " << (int)this << endl;
}
float Point::getX()
{
return x;
}
float Point::getY()
{
return y;
}
float Point::getZ()
{
return z;
}
void Point::setX(float X)
{
x = X;
}
void Point::setY(float Y)
{
y = Y;
}
void Point::setZ(float Z)
{
z = Z;
}
void Point::setXYZ(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}
istream &operator >>(istream &stream, Point &p)
{
float x, y, z;
stream >> x >> y >> z;
p.setXYZ(x, y, z);
return stream;
}
Point Point::operator =(Point &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
return *this;
}
class Vector : public Point
{
public:
Vector(float X=0, float Y=0, float Z=0);
Vector operator +(Vector &p);
};
Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
}
Vector Vector::operator +(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}
int main()
{
Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);
Vector vect3;
vect3 = vect1 + vect2;
cout << vect3 << endl;
}
|
|
|
|
|
OK - a couple of issues there. Firstly, your operator= isn't inherited because the signature of operator= in Point doesn't conform to what the generated operator= in Vector wants to see - it wants this:
Point& operator = (Point const &p);
i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway?
Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your operator= :
void getXYZ(float &X, float &Y, float &Z) const;
float getX() const;
float getY() const;
float getZ() const;
If you make those two changes, then your Vector's generated operator= will compile successfully.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you so much for your help. Looks like I have a bit of studying to do. I am not totally sure how this works but it does. Here is the fixed source in case someone needs to check it out.
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
void setXYZ(float X, float Y, float Z);
void setX(float X);
void setY(float Y);
void setZ(float Z);
void getXYZ(float &X, float &Y, float &Z);
float getX() const;
float getY() const;
float getZ() const;
Point& operator =(Point const &p);
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
x = f_x;
y = f_y;
z = f_z;
}
Point::~Point()
{
cout << "Destructing now!!! " << (int)this << endl;
}
float Point::getX() const
{
return x;
}
float Point::getY() const
{
return y;
}
float Point::getZ() const
{
return z;
}
void Point::setX(float X)
{
x = X;
}
void Point::setY(float Y)
{
y = Y;
}
void Point::setZ(float Z)
{
z = Z;
}
void Point::setXYZ(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}
Point& Point::operator =(Point const &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
return *this;
}
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}
istream &operator >>(istream &stream, Point &p)
{
float x, y, z;
stream >> x >> y >> z;
p.setXYZ(x, y, z);
return stream;
}
class Vector : public Point
{
public:
Vector(float X=0, float Y=0, float Z=0);
Vector operator +(Vector &p);
};
Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
}
Vector Vector::operator +(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}
int main()
{
Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);
Vector vect3;
vect3 = vect1 + vect2;
cout << vect3 << endl;
}
|
|
|
|
|