// page information $page_type = "t"; $page_title = "C++ Functions and Classes"; $page_keywords = "C++, C, functions, classes, functions and classes, prototype, declaration, programming, coding, code"; $page_description = "C++ Functions and classes tutorial. Find more tutorials and scripts at TheScripts.com, a programming and software development resource, directory and community."; $page_articletitle = "C++ Functions and Classes"; $page_next_url = ""; $page_next_anchor = ""; $page_prev_url = ""; $page_prev_anchor = ""; $page_author = "John Bourbonniere"; $page_byline = "Developer, PlayZion.com"; // site header include ($_SERVER["DOCUMENT_ROOT"]."/header.php"); // begin html ?>
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;
}
}
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;