Difference between revisions of "C++ Classes"
(One intermediate revision by the same user not shown) | |||
Line 8: | Line 8: | ||
Classes are declared with the <code>class</code> or <code>struct</code> [[Keyword (computer programming)|keyword]]. Declaration of members are placed within this declaration. | Classes are declared with the <code>class</code> or <code>struct</code> [[Keyword (computer programming)|keyword]]. Declaration of members are placed within this declaration. | ||
{| style="background:transparent; width:50%;" | {| style="background:transparent; width:50%;" | ||
− | | style="width:25%;" | < | + | | style="width:25%;" | <code> |
struct person | struct person | ||
{ | { | ||
Line 15: | Line 15: | ||
}; | }; | ||
− | </ | + | </code> |
− | |style="width:25%;" | < | + | |style="width:25%;" | <code> |
class person | class person | ||
{ | { | ||
Line 22: | Line 22: | ||
string name; | string name; | ||
int age; | int age; | ||
− | };</ | + | };</code> |
|} | |} | ||
Line 29: | Line 29: | ||
After one of these declarations (but not both), <code>person</code> can be used as follows to create newly defined variables of the <code>person</code> datatype: | After one of these declarations (but not both), <code>person</code> can be used as follows to create newly defined variables of the <code>person</code> datatype: | ||
− | < | + | <code> |
#include <iostream> | #include <iostream> | ||
#include <string> | #include <string> | ||
Line 52: | Line 52: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </code> |
Executing the above code will output | Executing the above code will output | ||
Line 63: | Line 63: | ||
Take the above <code>person</code> type as an example again: | Take the above <code>person</code> type as an example again: | ||
− | < | + | <code> |
class person | class person | ||
{ | { | ||
Line 82: | Line 82: | ||
*/ | */ | ||
} | } | ||
− | </ | + | </code> |
In the above example the <code>print()</code> function is | In the above example the <code>print()</code> function is | ||
Line 90: | Line 90: | ||
With the member function <code>print()</code>, printing can be simplified into: | With the member function <code>print()</code>, printing can be simplified into: | ||
− | < | + | <code> |
a.print(); | a.print(); | ||
b.print(); | b.print(); | ||
− | </ | + | </code> |
<br> | <br> | ||
Line 104: | Line 104: | ||
In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void. | In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void. | ||
{| style="background:transparent; width:25%;" | {| style="background:transparent; width:25%;" | ||
− | | style="width:25%;" | < | + | | style="width:25%;" | <code> |
// example: one class, two objects | // example: one class, two objects | ||
#include <iostream> | #include <iostream> | ||
Line 129: | Line 129: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </code> |
− | | style="width:25%;" | < | + | | style="width:25%;" | <code> |
// example: class constructor | // example: class constructor | ||
#include <iostream> | #include <iostream> | ||
Line 176: | Line 176: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </code> |
− | | style="width:25%;" | < | + | | style="width:25%;" | <code> |
rect area: 12 | rect area: 12 | ||
rectb area: 30 | rectb area: 30 | ||
− | </ | + | </code> |
|} | |} |
Latest revision as of 05:23, 15 October 2017
Wikipedia
Basic declaration and member variables
Classes are declared with the class
or struct
keyword. Declaration of members are placed within this declaration.
|
|
The above definitions are functionally equivalent. Either code will define objects of type person
as having two public data members, name
and age
. The semicolons after the closing braces are mandatory.
After one of these declarations (but not both), person
can be used as follows to create newly defined variables of the person
datatype:
- include <iostream>
- include <string>
using namespace std;
class person { public:
string name; int age;
};
int main() {
person a, b; a.name = "Calvin"; b.name = "Hobbes"; a.age = 30; b.age = 20; cout << a.name << ": " << a.age << endl; cout << b.name << ": " << b.age << endl; return 0;
}
Executing the above code will output
Calvin: 30 Hobbes: 20
Member functions
An important feature of the C++ class and structure are member functions. Each datatype can have its own built-in functions (referred to as methods) that have access to all (public and private) members of the datatype. In the body of these non-static member functions, the keyword this
can be used to refer to the object for which the function is called. This is commonly implemented by passing the address of the object as an implicit first argument to the function.
Take the above person
type as an example again:
class person
{
string name; int age;
public:
person() : age(5) { } void print() const;
};
void person::print() const {
cout << name << ";" << age << endl; /* "name" and "age" are the member variables. The "this" keyword is an expression whose value is the address of the object for which the member was invoked. Its type is const person*, because the function is declared const. */
}
In the above example the print()
function is
- Declared in the body of the class and
- Defined by qualifying it with the name of the class followed by
::
Both name
and age
are private (default for class) and print() is declared as public which is necessary if it is to be used from outside the class.
With the member function print()
, printing can be simplified into:
a.print();
b.print();
cplusplus
- http://www.cplusplus.com/doc/tutorial/classes/
- Constructors and destructors
Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. For example, what would happen if in the previous example we called the member function area() before having called function set_values()? Probably we would have gotten an undetermined result since the members x and y would have never been assigned a value.
In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void.
|
|
|