Cannot read the book example, special to ask

//File name: Points.cpp
//Function: demonstrate inherited ideas
//
#include<iostream.h>
class twoD
{
protected:
double x,y;
public:
twoD(double i,double j):x(i),y(j){}
void setX(double NewX){x=NewX;}
void setY(double NewY){y=NewY;}

double getX() const { return x;}
double getY() const {return y;}
};
class threeD:public twoD
{
private:
double z;
public:
//Inline constructor
//The base class constructors are not inherited
//Constructors multiplex threeD class a constructor for the twoD class, and through
//Member initialization list will value to the twoD constructor
threeD(double i,double j,double k):twoD(i,j){z=k;}
void setZ(double NewZ){z=NewZ;}
double getZ(){return z;}
};

void main()
{
twoD Obj2(1,1);
threeD Obj3(1,2,3);
cout<<"Coordinates for 2d object is:"<<endl;
cout<<Obj2.getX()<<","<<Obj2.getY()<<","<<endl;
cout<<"Coordinates for 3d object is:"<<endl;
cout<<Obj3.getX()<<","<<Obj3.getY()<<","<<Obj3.getZ()<<endl;
}


This example from void setX(double NewX){x=NewX}
void setY(double NewY){y=NewY}
void setZ(double NewZ){z=NewZ}Can still run, is this a few words is what to do with

Started by Theobald at November 06, 2016 - 4:51 PM

The statement you ask the function is to modify the private members x, y, Z interface by calling it, can modify their values.
You delete this a few words, but also to run because you didn't try to modify their values, but when you want to modify their worth when you have to think about how to modify the. Since you have seen examples of this book, the book also has introduced that the class data members and member function content.
Call the setX you pass in the main function () can modify the value of X.
But you can't directly access to the private data members of the X (for example, you write Obj2.x = 1; will be wrong), because you are in the class declaration in X is private. But if you put the private into public can directly access the.

Posted by Chad at November 17, 2016 - 5:00 PM

You can try to add before the cout statement:
Obj2.setX(3)
Obj3.setZ(5)
Then have a look the output results.

Posted by Warren at December 02, 2016 - 5:05 PM

Because you create objects without the use of these functions, so do not get rid of no effect.

Posted by Nicole at December 07, 2016 - 5:15 PM