First Class

This is a discussion on First Class within the C++ Programming forums, part of the General Programming Boards category; Hi, Im just strating to learn c++, and im currently at the stage of learning classes. My example below works ...

  1. #1
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28

    First Class

    Hi,

    Im just strating to learn c++, and im currently at the stage of learning classes. My example below works ok, but i have a few questions:

    1) where do i put the constructor and destructor and why? most of the examples i find are all different.

    2) When i try and create two objects of once class, my compiler throghs an errror (probably my code), i will comment out my second object louise and maybe you could offer me some advice why.

    3) Why do i have to do:

    cin>>a;
    Steve.SetAge(a);

    why can i not do:

    cin>>Steve.SetAge(a);

    4) lastly does my main look ok, or is it messy? Should i change the style?

    Look forward to your feedback. I have included my code below:
    Code:
    #include <iostream>
    
    class Employee
    {
    public:
    	void SetAge(int setage);
    	int GetAge(void);
    
    	void SetYearsOfService(int setyearsofservice);
    	int GetYearsOfService(void);
    
    	void SetSalary(int setsalary);
    	int GetSalary(void);
    
    private:
    	int age;
    	int yearsOfservice;
    	int salary;
    
    
    };
    
    void Employee::SetAge(int setage)
    {
    	age = setage;
    }
    
    int Employee::GetAge(void)
    {
    	return age;
    }
    
    void Employee::SetYearsOfService(int setyearsofservice)
    {
    	yearsOfservice = setyearsofservice;
    }
    
    
    int Employee::GetYearsOfService(void)
    {
    	return yearsOfservice;
    }
    
    void Employee::SetSalary(int setsalary)
    {
    	salary = setsalary;
    }
    int Employee::GetSalary(void)
    {
    	return salary;
    }
    
    
    	
    
    int main()
    {
    	int a, b, c;
    	Employee Steve;
                    Employee louise; // Second object?
    
                    // First Employee
    
    	std::cout<<"Welcome to the employee data register\n\n";
    	std::cout<<"1) Please enter employees age:\n";
    	std::cin>>a;
    	Steve.SetAge(a);
    
    	std::cout<<"2) Please enter employees years of service:\n";
    	std::cin>>b;
    	Steve.SetYearsOfService(b);
    
    	std::cout<<"2) Please enter employees salary:\n";
    	std::cin>>c;
    	Steve.SetSalary(c);
    	std::cout<<"\n\n\n";
    
                    std::cout<<Steve.GetAge()<<" is the age of employee\n";
    	std::cout<<Steve.GetYearsOfService()<<" is the years of service of
                    employee\n";
    	std::cout<<Steve.GetSalary()<<" is the salary of employee\n";
    
                   // Second Employee
    
                   std::cout<<"Welcome to the employee data register\n\n";
                   std::cout<<"1) Please enter employees age:\n";
                   std::cin>>a;
                   Louise.SetAge(a);
    
    	
    	return 0;
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> ) where do i put the constructor and destructor and why? most of the examples i find are all different.
    The constructor is like a 'function' and is generally a public member:

    Code:
    class thing{
    private:
      int val;
    
    public:
      // ...
      thing();
    };
    //...
    
    thing::thing() {
      //put code here
      val = 42;
    }
    It can be used to initialise variables. Say you wanted everything 0, or -1 or something before starting to do anything with the code.

    >> my compiler throghs an errror (probably my code), i will comment out my second object louise and maybe you could offer me some advice why.
    How do you mean it will comment it out? Do you mean that you have to comment out the second instance to make the code compile?

    >> 3) Why do i have to do: ...
    Because in making classes you are defining a new type. the computer knows how to read integers, characters, strings, floats etc from the input but you are making your own objects and the computer knows nothing about them.
    Code:
    class thing{
    public: // for the sake of this example.
      int one;
      char two;
    };
    If you were cin-ning an instance of thing to what would it assign the input? To the integer one or the character two?
    Also, what you posted is only necessary if the variables are not public. The above should work for the class thing above:
    Code:
    thing i_thing;
    cin >> i_thing.one;
    Alternatively you can overload the >> operator so you could "cin>> i_thing;" itself. Search the forums if you're interested.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Oh, I see:

    >> Employee louise;
    >> Louise.SetAge(a);

    C++ is case sensitive. louise.SetAge(a); should do the trick.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by RocketMan View Post
    1) where do i put the constructor and destructor and why? most of the examples i find are all different.
    Not sure what you mean by where? Where in the class definition?

    2) When i try and create two objects of once class, my compiler throghs an errror (probably my code), i will comment out my second object louise and maybe you could offer me some advice why.
    It doesn't throw an error. Just remember that everything is case sensitivt, so louise is not the same as Louise.

    3) Why do i have to do:

    cin>>a;
    Steve.SetAge(a);

    why can i not do:

    cin>>Steve.SetAge(a);
    cin>>Steve.SetAge(a); tries to input into the return from Steve.SetAge, which could work if the return type was anything other than void, I'm not sure, but nevertheless stupid, since you aren't storing that value, and thus, the returned value from SetAge would simply be destroyed.
    You must input data into variables.

    4) lastly does my main look ok, or is it messy? Should i change the style?
    I propose you keep indentation cosistent and don't mix spaces and tabs. Since you use tabs, stick with only tabs.
    And beware of two compile errors, marked and fixed as red:

    Code:
    #include <iostream>
    
    class Employee
    {
    public:
    	void SetAge(int setage);
    	int GetAge(void);
    
    	void SetYearsOfService(int setyearsofservice);
    	int GetYearsOfService(void);
    
    	void SetSalary(int setsalary);
    	int GetSalary(void);
    
    private:
    	int age;
    	int yearsOfservice;
    	int salary;
    
    
    };
    
    void Employee::SetAge(int setage)
    {
    	age = setage;
    }
    
    int Employee::GetAge(void)
    {
    	return age;
    }
    
    void Employee::SetYearsOfService(int setyearsofservice)
    {
    	yearsOfservice = setyearsofservice;
    }
    
    
    int Employee::GetYearsOfService(void)
    {
    	return yearsOfservice;
    }
    
    void Employee::SetSalary(int setsalary)
    {
    	salary = setsalary;
    }
    int Employee::GetSalary(void)
    {
    	return salary;
    }
    
    int main()
    {
    	int a, b, c;
    	Employee Steve;
    	Employee Louise; // Second object?
    
    	// First Employee
    
    	std::cout<<"Welcome to the employee data register\n\n";
    	std::cout<<"1) Please enter employees age:\n";
    	std::cin>>a;
    	Steve.SetAge(a);
    
    	std::cout<<"2) Please enter employees years of service:\n";
    	std::cin>>b;
    	Steve.SetYearsOfService(b);
    
    	std::cout<<"2) Please enter employees salary:\n";
    	std::cin>>c;
    	Steve.SetSalary(c);
    	std::cout<<"\n\n\n";
    
    	std::cout<<Steve.GetAge()<<" is the age of employee\n";
    	std::cout<<Steve.GetYearsOfService()<<" is the years of service of"
    		"employee\n";
    	std::cout<<Steve.GetSalary()<<" is the salary of employee\n";
    
    	// Second Employee
    
    	std::cout<<"Welcome to the employee data register\n\n";
    	std::cout<<"1) Please enter employees age:\n";
    	std::cin>>a;
    	Louise.SetAge(a);
    	
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Thanks for speedy reply. Something like this you mean? In c i try to keep main as small as possible and create lots of functions. As i can see it, main gets quite big in c++, if you are using classes - do you understand what i mean? How would i keep main smaller?

    Code:
    #include <iostream>
    
    class Employee
    {
    public:
    	void SetAge(int setage);
    	int GetAge(void);
    
    	void SetYearsOfService(int setyearsofservice);
    	int GetYearsOfService(void);
    
    	void SetSalary(int setsalary);
    	int GetSalary(void);
    
    private:
    	int age;
    	int yearsOfservice;
    	int salary;
    
    
    };
    
    void Employee::SetAge(int setage)
    {
    	age = setage;
    }
    
    int Employee::GetAge(void)
    {
    	return age;
    }
    
    void Employee::SetYearsOfService(int setyearsofservice)
    {
    	yearsOfservice = setyearsofservice;
    }
    
    
    int Employee::GetYearsOfService(void)
    {
    	return yearsOfservice;
    }
    
    void Employee::SetSalary(int setsalary)
    {
    	salary = setsalary;
    }
    int Employee::GetSalary(void)
    {
    	return salary;
    }
    
    
    	
    
    int main()
    {
    	int a, b, c;
    	Employee Steve;
    	Employee Louise;
    
    	//Employee One
    	std::cout<<"Welcome to the employee data register\n\n";
    	std::cout<<"1) Please enter employees age:\n";
    	std::cin>>a;
    	Steve.SetAge(a);
    
    	std::cout<<"2) Please enter employees years of service:\n";
    	std::cin>>b;
    	Steve.SetYearsOfService(b);
    
    	std::cout<<"3) Please enter employees salary:\n";
    	std::cin>>c;
    	Steve.SetSalary(c);
    	std::cout<<"\n\n\n";
    
                    std::cout<<Steve.GetAge()<<" is the age of employee\n";
    	std::cout<<Steve.GetYearsOfService()<<" is the years of service of 
                                                                                       employee\n";
    	std::cout<<Steve.GetSalary()<<" is the salary of employee\n";
    
    
                   // Employee Two
    	std::cout<<"Welcome to the employee data register\n\n";
    	std::cout<<"1) Please enter employees age:\n";
    	std::cin>>a;
    	Louise.SetAge(a);
    	
    	std::cout<<"2) Please enter employees years of service:\n";
    	std::cin>>b;
    	Louise.SetYearsOfService(b);
    
    	std::cout<<"3) Please enter employees salary:\n";
    	std::cin>>c;
    	Louise.SetSalary(c);
    	std::cout<<"\n\n\n";
    
                    std::cout<<Louise.GetAge()<<" is the age of employee\n";
    	std::cout<<Louise.GetYearsOfService()<<" is the years of service of 
                                                                                         employee\n";
    	std::cout<<Louise.GetSalary()<<" is the salary of employee\n";
    
    
    
    
    	
    	return 0;
    }

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You know you could recycle a, b and c to one variable. Won't change the size of main(), but that doesn't matter.

  7. #7
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Sorry the tabs are going wrong in main when i copy and paste, they should all be straight on left side.

    Thanks for your help.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Code:
    	std::cout<<Steve.GetYearsOfService()<<" is the years of service of 
                                                                                       employee\n";
    This one is still wrong. It must be:
    Code:
    	std::cout<<Steve.GetYearsOfService()<<" is the years of service of "
                                                                                       "employee\n";
    And you should still avoid spaces and keep indentation consistent.

    To narrow main, you could break out a function that sets properties of an employee and one that shows the properties on an employee. Then you can pass Steve and Louise to those functions and so main gets smaller.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User RocketMan's Avatar
    Join Date
    Dec 2007
    Posts
    28
    ok i will take your advice on board.

    So you are therefore saying in c++ you can have both c style functions and classes in the same .cpp file. I thought c++ was purely object oriented (sorry).

    Lastly, when developing bigger applications (not windows apps), do you create a new .cpp for each class and a seperate one for main - generally speaking?

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I generally do. It gives the project a more organised feel, which if it's big and complex is a good thing. Still. Doesn't mean the individual files are not long.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by RocketMan View Post
    So you are therefore saying in c++ you can have both c style functions and classes in the same .cpp file. I thought c++ was purely object oriented (sorry).
    No, it isn't Functions works just fine and is a very powerful tool.

    Lastly, when developing bigger applications (not windows apps), do you create a new .cpp for each class and a seperate one for main - generally speaking?
    Typically, yes, since it makes it easier to modify your classes when you need to.
    But you don't need a separate file for main; you can use separate files for different parts of your application. One for your typical application code, one each for all your dialogs, etc.
    This is irregardless of what type of application you're creating.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    C++ is multi-paradigm. http://en.wikipedia.org/wiki/Multi-p...mming_language

    you can use it like java (purely object oriented) or like C (purely imperative) or anywhere in between.

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, 08: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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21