C++ Functions and Classes
Developer, PlayZion.com
Using Functions and Classes with C++
Functions
Function Prototype:
<return class> <function name> (class1> <name1>, ..., <classN> <nameN>);
Function Declaration:
<return class> <function name> (class1> <name1>, ..., <classN> <nameN>);
{
<function body goes here>
}
Example:
int Max (int num1, int num2);
{
if (num1 >= num2) {
return num1;
}
else {
return num2;
}
}
Classes:
Class Declaration:
class <class name>
{
private:
<private member data>
<private member functions>
public:
<public member data>
<public member functions>
};
For example,
class Rectangle {
private:
float length;
float width;
public:
void setLength(float inLength);
void setWidth(float inWidth);
float getLength();
float getWidth();
float getArea();
}
With this rectangle declaration, we can use the following code:
Rectangle square;
square.setLength(2.0);
square.setWidth(2.0);
cout << "Rectangle has length " << square.getLength() << endl;
cout << "Rectangle has width " << square.getWidth() << endl;
cout << "Area of rectangel is " << square.getArea() << endl;
