Thread: understanding user-defined classes

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    understanding user-defined classes

    I'm trying to learn about "user defined classes" and don't understand what they're for. Once I see a project compile it seems to click with me so I typed up the source file and matching "user defined class definition" that is in the user-defined classes chapter of my book. This file is a "counter class" of some sort. I put both the .cpp and .h files into one project and it didn't work. I was hoping to see what the advantage of a user-defined class was by seeing it work, but no luck. I tried another set that deals with "cube" I found on the net and it didn't work as well, so I must be missing something. As far as what I did to get these results: I added the header file by clicking on "add new item" under the tree. all that is there is a counter.h file and in the same project is a .cpp file which is included below. I have also included the info I found on the net; it is in the second half of this post.

    Code:
    #ifndef COUNTER_H	
    #define COUNTER_H
    
    class counter
    {
    public:
    
    	counter();
    	counter(int);
    
    	void increment();
    
    	void decrement();
    	void setCount(int);
    
    	void setMaxValue(int);
    
    	int getCount() const;
    
    	int  getMaxValue() const;
    
    private:
    
    	int count;
    	int maxValue;
    
    };
    
    #endif // COUNTER_H


    Code:
    
    #include <iostream>
    #include "header1.h"
    using namespace std;
    
    int main ()
    {
    
    	counter c1;
    
    	counter c2(10);
    
    	c1.setCount(50);
    	c1.decrement();
    	c1.decrement();
    	c1.increment();
    	cout << "Final value of c1 is " << c1.getCount() << endl;
    
    	c2.increment();
    	c2.increment();
    	c2.decrement();
    	cout << "Final value of c2 is " << c2.getCount() << endl;
    
    	return 0;
    
    }
    Here are the errors that come up:

    1>------ Build started: Project: classPractice, Configuration: Debug Win32 ------
    1>Compiling...
    1>classPractice.cpp
    1>Compiling manifest to resources...
    1>Linking...
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: int __thiscall counter::getCount(void)const " (?getCount@counter@@QBEHXZ) referenced in function _main
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::increment(void)" (?increment@counter@@QAEXXZ) referenced in function _main
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::decrement(void)" (?decrement@counter@@QAEXXZ) referenced in function _main
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::setCount(int)" (?setCount@counter@@QAEXH@Z) referenced in function _main
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: __thiscall counter::counter(int)" (??0counter@@QAE@H@Z) referenced in function _main
    1>classPractice.obj : error LNK2019: unresolved external symbol "public: __thiscall counter::counter(void)" (??0counter@@QAE@XZ) referenced in function _main


    ==================================================
    here is the info for the cube project:

    Code:
    #ifndef CUBE_H#define CUBE_Hclass Cube{public:	Cube();	~Cube();	
    void setSide(double s);	
    double getSide();	
    double Area();	
    double Volume();	
    void Properties();private:	double Side;
    };#endif

    Code:
    #include <iostream>
    #include "cube.h"
    
    Cube::Cube(){}Cube::~Cube(){}
    
    void Cube::setSide(double s)
    {	
    Side = s <= 0 ? 1 : s;
    }
    
    double Cube::getSide()
    
    {	
    	return Side;
    }
    double Cube::Area()
    {
    	
    return 6 * Side * Side;
    
    }double Cube::Volume()
    {	
    return Side * Side * Side;
    }void Cube::Properties()
    {	
    cout << "Characteristics of this cube";	
    cout << "\nSide   = " << getSide();	
    cout << "\nArea   = " << Area();	
    cout << "\nVolume = " << Volume() << "\n\n";
    }

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you never implemented those functions.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you have defined the functions in the class definition in your header file, you have called on the the functions in main() to use them, but there are no other instructions anywhere that actually code what the functions calls 'do'

    i.e you would have more blocks of code in another source file (counters.cpp??), or in main if you like for a small project,
    an example like >



    Code:
    void counter::decrement()
    }
         count --;
    }
    now your member variable 'count' is decremented by the member function 'decrement()' when it is called in main(), this is the implementation of your function.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    the example in the book doesn't have that. I looked at a different class example in the book and it has a few .cpp files. one is called a driver function and the other is called an implementation file; both have to do with a fraction class and has a fraction class definition. does this mean my tree will have 2 .cpp files and one header file? All of the programs I have done so far have just one .cpp file with no add'l header file.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    the names of the files you saw in your book are immaterial, it is a name only, despite them having titles that suggest otherwise.

    Generally....>
    Say you have a project and you want it divided into seperate modules to make it more manageable, say your program contains a class that deals with incrementing and decrementing counters, and the class contains several functions that can do these operations, you might also have some non-member functions that deal with your counters (for the sake of example).

    the above items you will add to a header file you create, called whatever... 'counter.h', in this you define your class , its types of member variables, and define the member functions, i.e. list their return type, name, and any arguments they carry.
    You then also define your non-member functions in the same way in this file.
    you comment your functions to say what they do.

    Then you have your CPP source file that is created as a natural relative of this header, because it contains all the actual 'doing' part of the functions and would otherwise have cluttered up your class definition and just not been very cool.
    the source file is 'CountSource.cpp'

    then in main() you include your counter.h, your source file must be added as part of your project and check the box so it is included in the build.

    in main you would probably actually be calling on your functions in the source files but obviously this occurs anywhere.

    then you find you have another whole set of functions that draw to the screen in your program, so your create a 'graphics.h' header and define all your drawing functions, then you create GraphicSource.cpp to hold all the actual implementation of the functions...you add the header to main..and the other source files..etc etc

    it goes on if you want...> you find there are other related classes to your counter...so you create a 'namespace'...

    this approach works for me anyhow....so far...
    Last edited by rogster001; 11-24-2009 at 02:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. User defined Variable name
    By mat429 in forum C Programming
    Replies: 4
    Last Post: 08-28-2008, 08:07 AM
  3. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  4. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM