Thread: Class help.

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    7

    Question Class help.

    I've been reading various help sites on C++ here and there, and found one I liked, although it's a bit older, and slightly out of date, but so far on the right track to helping me further my understanding... However...

    I was reading about classes in it, and it gave an example that it (to me) did a very poor job of explaining, and for the life of me I cannot reproduce it in my own code to try and even half way understand how it works. Their code in it compiles, so I know it works, I just don't know how, why, and what it's doing in it.

    http://newdata.box.sk/bx/c/htm/ch06.htm#Heading40

    That is the help file link. If somebody could even half way explain the whats and whys to this, that'd be great. If you look through the help file there, I've read and can do whatever is above and before that point, just so you know what I can understand.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.
    Last edited by 7stud; 01-20-2007 at 09:30 PM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    Hmm... I just don't get this.. Lines 28-31 is not how I learned that methods can be made. I understand those are inline methods, but it appears it's being declared as a Point type, which, I don't know why, or how, or what it's supposed to do.. It just isn't explained.

    46-49 appears to be making objects of the Point class, but then 28-31 shows that the inline methods set the so-called objects to what Location equals, as if they were seperate variables, and not objects. Also, the methods not only do this, they also use "Point Location" as their parameter.

    - Edit - My bad, I've been sort of rushy, things to do, so I'm missing details. It is lines 28-31 saying inline methods are a Point, returning an object as a value, and then 33-36 using "Point Location" as paramters, and setting "objects" as variables. I assume part of my rushiness is contributing to my lack of understanding, I can't imagine this much work into a file to not explain something so thoroughly. -

    So, am I to understand that you can use the objects themselves as a variable? And if so, how does one declare the data type for it? How would Point return a value as shown it supposivly can in those inline methods? Does that mean it can only return Point "objects"? Again objects themselves being used as variables.

    I understand what you're doing with those classes, 7stud, that makes sense. The p object was created inside of the Data class and is being used by it to access the p's members.

    Sorry I wasn't so clear with my problems before, I wasn't sure how to define them until now.
    Last edited by Something; 01-20-2007 at 01:43 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So, am I to understand that you can use the objects themselves as a variable?
    Yes. You can use Point in almost every way that you could use int or double. A class creates a new type that you can use as the type of a variable (including member variables). You can also use this new type as the return type for a function (like the functions on lines 28-31). You can use this new type as the type of a function parameter (like in lines 33-36).

    If you had some other code, you could use Rectangle as a new type as well. That's the power of classes, they define new types.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    Bleh.. All I can say at this point is that help file made a big mess of what probably should be something easy, if it only explained it all. You don't introduce things without explaining, and then just move off from the subject entirely..

    I'm still confused with the entire concept of it. I've read through it multiple times over the last 24 hours, and still very little progress in learning it.

    Thanks for the help so far, hope to get this cleared up soon.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but then 28-31 shows that the inline methods set the so-called objects to what Location equals, as if they were seperate variables, and not objects.
    I think you must be talking about these lines:
    Code:
    33:        void SetUpperLeft(Point Location)  {itsUpperLeft = Location;}
    34:        void SetLowerLeft(Point Location)  {itsLowerLeft = Location;}
    35:        void SetUpperRight(Point Location)  {itsUpperRight = Location;}
    36:        void SetLowerRight(Point Location)  {itsLowerRight = Location;}
    You can do the following, right?
    Code:
    int n = 10;
    int y = 30;
    n=y;
    What value does n have stored in it?

    You can do the same thing with Point objects and Point variables. When you write:

    Point p;

    that innocuous looking statement sets off a whole chain of events. First a variable named p is created. Then the default constructor for the Point class is called to create a Point object. Finally, the Point object is stored in the variable p. Try changing the default constructor in Point to this:
    Code:
    Point()
    {
    	cout<<"Default constructor called."<<endl;
    			
    }
    and then in main() write:

    Point p;

    and see what happens.

    I think what is confusing you is the fact that there are no "object literals" in C++. With int's, there are int variables, like:

    int n;

    and "int literals" like 10, 23, 0. You assign an int literal to an int variable like so:

    int n = 10;

    int's like 10, 23, 0 are called "int literals" to distinguish them from int variables(which vary or change). In C++, there are also char variables which can be assigned "char literals" like 'a', string variables which can be assigned string literals like "hello world", and double variables which can be assigned double literals like 10.5. However, in C++ there are no object literals. You can't write:

    Point p = {int x = 0, int y = 0};

    You can only write:

    Point p;

    and then C++ does the rest: it creates the object using the default constructor and then stores the object in the variable. You can also write:

    Point c(10, 20);

    then the constructor that takes two ints as arguments in the Point class is called, an object is created, and the object is stored in c. Next, if you write this:

    p = c;

    A copy of the Point object stored in the variable c will be stored in the variable p.


    Lines 28-31 is not how I learned that methods can be made.
    The absolute simplest way to define a method in a class is like this:
    Code:
    class Point
    {
    	public:
    		int getX()
    		{
    			return x;
    		}
    
    	private:
    		int x;
    		int y;
    };
    There is no simpler way, and to start out that is the way it is usually taught. You can also write that with a different format:
    Code:
    class Point
    {
    	public:
    		int getX() {return x;}
    
    	private:
    		int x;
    		int y;
    };
    The only difference is that a lot of whitespace was removed.

    I understand those are inline methods, but it appears it's being declared as a Point type, which, I don't know why, or how, or what it's supposed to do.. It just isn't explained.
    In the Rectangle class you have this:
    Code:
    class Rectangle
    {
    	....
    	Point  GetUpperLeft() const { return itsUpperLeft; }
    	...
    };
    You can rearrange that line to look like this:
    Code:
    Point  GetUpperLeft() const
    {
    	return itsUpperLeft;
    }
    All functions must declare their return type, the function name, and any parameters the function has(this one also has a const modifier after it, but the explanation of that is a more advanced topic) . This function declares that it will return a value that is of type Point. Since the Point class was defined earlier, Point is now a type, just like int and double. Practically, what that means is that the function is going to return a Point object when you call it. Therefore, when you call the function you will typically write something like this:
    Code:
    Point p = myRectangle.GetUpperLeft();
    The variable p will store the Point object returned by GetUpperLeft(). p is a variable, just like any other variable, so you can change its value at any time. For instance, later you could write:
    Code:
    Point c:
    p = c;
    46-49 appears to be making objects of the Point class

    45: private:
    46: Point itsUpperLeft;
    47: Point itsUpperRight;
    48: Point itsLowerLeft;
    49: Point itsLowerRight;
    50: int itsTop;
    51: int itsLeft;
    52: int itsBottom;
    53: int itsRight;
    No, that isn't correct. Nothing is created. All those lines say is that if you create a Rectangle object, then it will have Point objects named itsUpperLeft, etc. inside of it. I assume you have studied arrays by this point. An array can only store things that are one type inside of it, like int's or doubles. All the elements that you put into an array have to be the same type: the type that was specified when the array was declared. An object of a class is like an array in that it can store data inside of it. But unlike an array, an object of a class can store different types of things in it. For instance, an object of a class can store integers like itsTop and itsLeft--but it can also store things of type Point, i.e. Point objects, inside of it.

    I understand what you're doing with those classes, 7stud, that makes sense. The p object was created inside of the Data class and is being used by it to access the p's members.
    Then try rereading Note (5). Try the example and understand how it works. That is something that will repeatedly trip you up when programming with C++ no matter how experienced you are. Experienced programmers just understand the error message immediately when it happens and make the proper corrections to their code.

    I can't imagine this much work into a file to not explain something so thoroughly. -
    I started learning C++ with that book(many years ago), and after 1 week I threw it in the trash because I was so frustrated by its poor explanations. People say current editions are better, but the online stuff is probably pretty old. I switched to Ivor Horton's Beginning C++, and I thought it was excellent. There are no holes in the explanations in that book(although I would suggest skipping the overly complex section in chapter 3 on bitwise operators--nothing in the rest of the book builds on that).

    One book that's highly recommended is "Accelerated C++" which takes a much different approach to C++ than any other book. It's also fairly short, so it doesn't require a huge investement in time, like most C++ books do.
    Last edited by 7stud; 01-21-2007 at 12:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM