Thread: Confusion with Constructors in classes

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Confusion with Constructors in classes

    Hello, everyone. I currently am having some trouble with C++, mostly the Object Oriented aspect of it. I have been working on a series of problems on one homework assignment and I am left stumped to the point that everything I have learned somehow makes no sense.(hard to explain) I had to take a cpp file which has the class and the main in it, and then split it into three files. a header, main and core file. The core file will use a constructor which calls on two variables(i think this is the right term for this). The header will be like any header would, and the main would execute the whole program. The problem I am currently having which maybe caused by another error but right now I only have two. I somehow am unable to call upon the function which has a return of Void. I'm running the Microsoft Visual C++ 2008 Express Edition compiler incase that makes any difference. Below I'll post the code for each of the files. I have another problem from the same assignment but this one has been plaguing me right form the start. By the way, I am in a Game Programming degree course in case that helps in where I am coming from.
    I have looked online and seen mixed answers on how to do this, and any help i tried to get from other sites, was not really helpful. I've gone through tutorials both online and in my course book, but somehow something is not adding up right. Thank you ahead of time for any help I get, I'll surely be looking on here more and asking more questions to future homework assignments.

    Main file:

    Code:
    #include <iostream>
    using namespace std;
    #include "Asteroids.h"
    
    int main()
    
    {
    	Asteroid firstAsteroid;
    	Asteroid secondAsteroid;
    	firstAsteroid.displayStats(1,20);
    	secondAsteroid.displayStats(3,4);
    
    char wait;
    cin>> wait;
    
    	
    return 0;
    }
    Core file:
    Code:
    #include <iostream>
    using namespace std;
    
    class Asteroid
    {
    	int astSize;
    	int astSpeed;
    	int AsteroidNumber;
    public:
    	Asteroid ( int Size, int Speed, int AsteroidNumber )
    	{
    		setSpeed( Size);
    		setSize (Speed);
    	}
    	int getSize()
    	{
    		return astSize;
    	}
    	int getSpeed()
    	{
    		return astSpeed;
    	}
    	void displayStats( int AsteroidNumber)
    		{
    		cout<< "Asteroids " << endl;
    		cout<< " Asteroid number " << AsteroidNumber << endl;
    		cout<< " Size " << astSize << endl;
    		cout<< " Speed " << astSpeed << endl;
    	}
    	void setSize(int Size)
    	{
    		astSize = Size;
    	}
    		void setSpeed(int Speed)
    	{
    		astSpeed = Speed;
    	}
    };

    header:

    Code:
    #define Asteroids_h
    
    class Asteroid
    {
    	public:
    	int astSpeed;
    	int astSize;
    	int AsteroidNumber;
    	
    
    	Asteroid () {}
    
    
    void displayStats(int astSize, int astSpeed, int AsteroidNumber);
    void setSize( int astSize);
    void setSpeed(int astSpeed);
    
    int setSpeed()
    {
    	return astSpeed;
    }
    int setSize()
    {
    	return astSpeed;
    }
    
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There's a few problems with your code. Your main file and your header file are almost correct (except there are a couple typos/minor errors in the header: for example you use set instead of get twice).

    The cpp file is where you need help. I'd suggest reviewing what the source file looks like again, because you need to start from scratch on that one.

    The first thing to remember is that the class Asteroid part doesn't belong there, it is already in the header. Then you have to remember how to indicate that a function you are implementing in the source file belongs to the class you define in the header. Give the source file another shot and see where you are.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Thank you for answering. So I delete the class asteroid part from the core cpp file(Asteroids.cpp) and then go on from there? It's late and I've done enough late nights. Thankfully with spring break I can focus on homework and getting caught up. Thank you again, I'll give it a shot tomorrow and see how it works.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As an "example"

    foo.h
    Code:
    #ifndef foo_h
    #define foo_h
    class foo
    {
        int whatever;
    public:
        foo();  // Constructor
        void SetWhatever(int val);
        int GetWhatever() const;
    };
    #endif
    foo.cpp
    Code:
    #include "foo.h"
    
    foo::foo()
    {
        // Code for constructor goes here
    }
    
    void foo::SetWhatever(int val)
    {
        whatever = val;
    }
    
    int foo::GetWhatever() const
    {
        return whatever;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "foo.h"
    
    int main()
    {
        foo bar;
    
        bar.SetWhatever(10);
    
        std::cout << bar.GetWhatever() << std::endl;
    
        return 0;
    }
    Man, I've been doin' PL/SQL too long, I keep wanting to do var := value instead of var = value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Thank you hk, so I can just add the Class name and the :: and for what I want to call, have it as a const, and it should work? as you showed in your example.

    foo::GetWhatever() const

    like that there. I just want to try to understand it, as it was never really explained well in any books that I've gone through. (blame the retentiveness to want to know 'exactly' so I'm perfectly clear)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The const depends on the function. I don't think you should worry about it here, it isn't necessary with your code. But the class name and the :: is a big part of what you'll need in your source file.

    We hesitate to give you the "exact" answer because we don't like to do homework and because people generally learn better if they figure it out rather than seeing the final answer.

    Have you given it another try yet? Show what you've got, even if it doesn't compile completely. Also, have you tried to find and fix the bugs in the header?

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    I fixed the typos I think that are right in the header, I'm just starting on the cpp file trying to add the class:: before each of the functions(I think that is correct.) I just want to try to work with what I have and see if I can make it work. I don't mind working for it, I just am at the level where all help is welcome.

    Edit: code for cpp

    Code:
    #include <iostream>
    #include "Asteroids.h"
    using namespace std;
    
    class Asteroid
    {
    	int astSize;
    	int astSpeed;
    	int AsteroidNumber;
    public:
    	Asteroid ( int Size, int Speed, int AsteroidNumber )
    	{
    		setSpeed( Size);
    		setSize (Speed);
    	}
    	int Asteroid::getSize()
    	{
    		return astSize;
    	}
    	int Asteroid::getSpeed()
    	{
    		return astSpeed;
    	}
    	void Asteroid::displayStats( int AsteroidNumber)
    		{
    		cout<< "Asteroids " << endl;
    		cout<< " Asteroid number " << AsteroidNumber << endl;
    		cout<< " Size " << astSize << endl;
    		cout<< " Speed " << astSpeed << endl;
    	}
    	void Asteroid::setSize(int Size)
    	{
    		astSize = Size;
    	}
    	void Asteroid::setSpeed(int Speed)
    	{
    		astSpeed = Speed;
    	}
    };
    Last edited by Bparm; 03-11-2010 at 01:23 PM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    const just means that it's a constant member function, that is, it is a function that is guaranteed not to modify the object it's called for. The GetWhatever function in my example simply returns a value and so does not modify the object, therefore it should be declared as const but it isn't an absolute requirement to do so. I was simply trying to demonstrate the concept of const correctness. The SetWhatever function alters the value of the object's whatever member variable and is thus not a const member function. If a const member functions calls other member functions those must be const as well.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  2. Classes - Confusion
    By Kane in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2005, 10:55 AM
  3. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  4. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM
  5. Replies: 2
    Last Post: 01-15-2002, 06:00 PM