C++ Classes

From emboxit
Jump to: navigation, search

Wikipedia

Basic declaration and member variables

Classes are declared with the class or struct keyword. Declaration of members are placed within this declaration.

struct person {

 string name;
 int age;

};

class person { public:

 string name;
 int age;

};

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:

  1. include <iostream>
  2. 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

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.

// example: one class, two objects

  1. include <iostream>

using namespace std;

class CRectangle {

   int x, y;
 public:
   void set_values (int,int);
   int area () {return (x*y);}

};

void CRectangle::set_values (int a, int b) {

 x = a;
 y = b;

}

int main () {

 CRectangle rect, rectb;
 rect.set_values (3,4);
 rectb.set_values (5,6);
 cout << "rect area: " << rect.area() << endl;
 cout << "rectb area: " << rectb.area() << endl;
 return 0;

}

// example: class constructor

  1. include <iostream>

using namespace std;

class CRectangle {

   int width, height;
 public:
   CRectangle (int,int);
   int area () {return (width*height);}

};

CRectangle::CRectangle (int a, int b) {

 width = a;
 height = b;

}

int main () {

 CRectangle rect (3,4);
 CRectangle rectb (5,6);
 cout << "rect area: " << rect.area() << endl;
 cout << "rectb area: " << rectb.area() << endl;
 return 0;

}


//---------------------------------------------------------------- // example: class constructor inside class

  1. include <iostream>

using namespace std;

class CRectangle {

   int width, height;
 public:
   CRectangle::CRectangle (int a, int b) {width = a; height = b;}
   int area () {return (width*height);}

};

int main () {

 CRectangle rect (3,4);
 CRectangle rectb (5,6);
 cout << "rect area: " << rect.area() << endl;
 cout << "rectb area: " << rectb.area() << endl;
 return 0;

}

rect area: 12 rectb area: 30