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++"