C++ Notes Series 1 (Class)

I am starting to use C++ more. I used to be a Java programmer. This series of notes would be my first venture in the real of C++.

The reason to use C++, I use it mostly as an extension of C. The features that I look for a re classes, functors, simple templates. Most of the core of the program I write are in C. But C++ makes all the other parts more organized.

I used to be a Java programmer, so much of the post would be from a Java programmer’s perspective.

This is mostly based on this tutorial here
http://www.cplusplus.com/doc/tutorial/classes/

Classes:

One important concept is that “class” is an “extension” of data structures. The syntax is mostly based on struct {}. The biggest difference is that now you have the option adding functions that go with the data structure. In C, we would just define a separate function that takes as input the data structure.

Another difference is the ability to add access specifiers such as public, private and protected. However, that is pretty much the same as Java.

An example of a class

class Rectangle {

   int width, height;

  public: void set_values (int,int);

  int area (void);

} rect;

Methods

The definition of the method can happen inside the class definition or out side the class definition. 

For example, we can do

class Rectangle {

int width, height;

public:

void set_values (int x,int y) {

width = 2*x;

height = 2*y;

};

int area() {return width*height;}

};

 

which defines all the methods (function) within the definition of the class.

Or we could define it outside

// classes example
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

//defined outside the class definition
void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

The main difference is that function defined within the declaration is automatically considered inline, the function defined outside is NOT. quoting the link below. So I guess it might be better to always try to define it inside, or otherwise, use the inline specifier. 

"
The only difference between defining a member function completely within the class definition or to just include its declaration in the function and define it later outside the class, is that in the first case the function is automatically considered an inline member function by the compiler, while in the second it is a normal (not-inline) class member function. This causes no differences in behavior, but only on possible compiler optimizations.
"

Constructor 

same as in Java, a special method that gets called everytime a new object of the class have been created. It is used to allocate internal variables/states of the object. 

Something special about the C++ constructor is that there is a special member initialization method, 

You can initialize Rectangle with the following constructor

Rectangle:Rectangle(int x, int y): width(x), height(y) {}

(Since the functions does nothing more than initialize the members, the function body is empty)

instead of the more traditional

Rectangle:Rectangle(int x, int y) { width = x; height = y;}
Advertisements
This entry was posted in Tools, Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s