Hi,
As you learned earlier, classes can contain data. For instance,
Code:
class Data
{
public:
Data(int n, double d) //constructor function
{
num = n;
distance = d;
}
void show()
{
cout<<num<<" "<<distance<<endl;
}
private:
int num;
double distance;
};
and you can create objects of the class like this:
Code:
#include<iostream>
using namespace std;
//class definition here
int main()
{
Data myData(10, 15.4);
myData.show();
return 0;
}
The class Data has two member variables: num and distance. num is of type int and distance is of type double. Furthermore, the variable myData in main() is said to be of type Data. In other words, a class defines a new type.
A class like Data is not restricted to having member variables of just type int and double. It can contain other types. So if you first define a class called Point, then you can define the Data class to contain member variables of type Point. Here is an example of a Point class:
Code:
class Point
{
public:
void setX(int xPos)
{
x = xPos;
}
void setY(int yPos)
{
y=yPos;
}
void display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
private:
int x;
int y;
};
On a typical x, y axis coordinate system, a point can be defined by its x and y coordinates. An object of the Point class will represent one point. Now, if you want, you can define the Data class like this:
Code:
class Data
{
public:
Data(int n, double d, int xPos, int yPos) //constructor
{
num = n;
distance = d;
p.setX(xPos);
p.setY(yPos);
}
void show()
{
cout<<num<<" "<<distance<<endl;
p.display();
}
private:
int num;
double distance;
Point p;
};
You can try the code out using:
Code:
int main()
{
Data myData(10, 15.4, 3, 4);
myData.show();
return 0;
}
Note:
1) The definition of the Point class has to come first. When the compiler sees Point mentioned inside Data, the compiler needs to have already been introduced to Point.
2) Functions in a class can access the private member variables of the class--see Data's show() function.
3) Since p is a Point, p can be used to call any public function in the Point class--see Data's show() function.
4) You may wonder how a Point object can be created since Point doesn't define a constructor function. If you don't define a constructor function in a class, a default constructor is automatically provided for the class. The default constructor creates the member variables, but it doesn't assign any values to them(they will contain junk values).
5) There is a lot of stuff that happens invisibly behind the scenes with constructors, which can trip you up until you understand how everything works. For instance, when the Data constructor is called, the first thing that happens is all the member variables for Data are created. In order to create the Point member variable p, Point's default constructor is called. Finally, inside the Data constructor the member variables are assigned values.
Now, if you define a constructor inside the Point class, for instance to initialize the points with given values, then the class will no longer be provided with a default constructor. Yet, the Data constructor will cause Point's default constructor to be called for p. The result will be an error--there is no default constructor in Point. So, the solution is to define a constructor that has no parameters to serve as the default constructor. Inside the default constructor you define, if you want, you can initialize all the member variables to say 0. That would look like this:
Code:
Point()
{
x = 0;
y = 0;
}
To demonstrate this phenomenon, I suggest you add a constructor to the Point class that takes two integers. Use those integers to set the values of the x and y member variables. Write some code in main() to create a Point object that calls the constructor with 2 ints. Then try to create a Data object. You should get an error saying there is no default constructor in Point.
Post any specific questions you have.