Thread: I'm having a really hard time understanding classes.

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    I'm having a really hard time understanding classes.

    As the subject reads, i'm having a huge amount of trouble understanding classes. The tutorials on this sight are good, but they don't give enough simple examples for me to follow on. What I'm having trouble with is calling a function that belongs to the class in main().

    The following program isnt sopposed to do anything at all, its just used to understand how classes work.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream.h>
    class aclass
     {
      public:
       aclass(int num1, int num2);
       ~aclass();
       int aclass::afunction();
       
      };
      aclass::aclass(int num1, int num2)
       {
        num1, num2 = 2;
        cout<<num1*num2;
       }
      aclass::~aclass()
       {}
      int aclass::afunction()
       {
        cout<<"do this stuff";
       }
       
    int main(int argc, char *argv[])
    {
      aclass theclass;
      
      theclass.afunction();
      
      system("PAUSE");	
      return 0;
    }

    The code doesnt compile if I call afunction() inside main. Help needed. Thankyou to all in advance.

    Also, can someone direct me to more class (or full C++) tutorials I can find on the web. Using more then one source helps me learn better, and im having trouble finding another at the moment.
    ~matt~

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    1. You have one (only one) constructor,which takes 2 int parameters.
    But you call the absent default constructor.
    2. afunction is declarred to return an int,but you forgot the return.

  3. #3
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Code:
    int aclass::afunction();
    you dont make it like that, you just can go,

    Code:
    int afunction();
    then when you want to define it, thats when you do

    Code:
    int aclass::afunction(){
    
    //.....whatever you want
    
    return 0;// or whatever int you need.
    };

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's a simple first class, complete with a driver program for you to check out.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    class aclass
     {
      public:
       aclass(int num1, int num2);
       ~aclass();
       int aclass::afunction();
       
      };
    First of all, your class has no data members, which is not right because your constructor has two int parameters, which suggests you should have two int data members.

    Here is a simple class with two data members that doesn't do anything at the moment:
    Code:
    class aclass
    {
    public:
    	aclass(int num1, double num2); //constructor declaration
    
    private:
    	int num1;
    	double num2;
    };
    You can either define functions, including constructors, inside the class or outside the class. Usually, for short functions, you will define them inside the class.

    Function definition inside the class:
    Code:
    class aclass
    {
    public:
    	aclass(int a, double b)//constructor definition
    	{
    		num1 = a;
    		num2 = b;
    	}		
    
    private:
    	int num1;
    	double num2;
    };
    Function definition outside the class:
    Code:
    class aclass
    {
    public:
    	aclass(int a, double b);//constructor declaration
    	
    private:
    	int num1;
    	double num2;
    };
    
    aclass::aclass(int a, double b)//constructor definition
    {
    	num1 = a;
    	num2 = b;
    }
    Ok, that's a simple example of a constructor. Note: a constructor does not have a return type, so it's unique in that respect. Now let's make the class do something. Let's add a function that displays the data members:
    Code:
    #include <iostream>
    using namespace std;
    
    class aclass
    {
    public:
    	aclass(int a, double b);//constructor declaration
    	void print()
    	{
    		cout<<"Here are the data members:\n";
    		cout<<num1<<" "<<num2<<endl;
    	}
    
    	
    private:
    	int num1;
    	double num2;
    };
    
    aclass::aclass(int a, double b)//constructor definition
    {
    	num1 = a;
    	num2 = b;
    }
    
    int main()
    {
    	aclass a(3, 4.25); //calls constructor
    	a.print(); //calls function print()
    
    	return 0;
    }
    Now, let's add one more function that multiplies the data members by 2:
    Code:
    void multX2()
    	{
    		num1 *= 2;
    		num2 *= 2;
    	}
    This time we will define the function outside the class:
    Code:
    #include <iostream>
    using namespace std;
    
    class aclass
    {
    public:
    	aclass(int a, double b);//constructor declaration
    	void print()
    	{
    		cout<<"Here are the data members:\n";
    		cout<<num1<<" "<<num2<<endl;
    	}
    	void multX2();
    		
    private:
    	int num1;
    	double num2;
    };
    
    void aclass::multX2()
    {
    	num1 *= 2;
    	num2 *= 2;
    }
    
    aclass::aclass(int a, double b)//constructor definition
    {
    	num1 = a;
    	num2 = b;
    }
    
    int main()
    {
    	aclass a(3, 4.25);
    	a.print();
    
    	a.multX2();
    	a.print();
    
    	return 0;
    }
    Since all the functions are short, I would have included all the definitions inside the class. Instead, I defined the constructor and the print() function outside the class so you could see the format of a constructor versus a general class function.

    I hope that helps. If you need more information, just got to google and search for: "classes C++"
    Last edited by 7stud; 05-25-2003 at 01:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm having a hard time making a .exe from a .cpp
    By manugarciac in forum C++ Programming
    Replies: 10
    Last Post: 05-13-2009, 04:40 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. Hard to get classes
    By Unregistered in forum C++ Programming
    Replies: 36
    Last Post: 12-05-2001, 04:17 AM