Class and Objects, Placing Classes in Separate Files

Posted on April 18, 2013. Filed under: C++ |

C++ support Class and Objects. A Class need a access specifier.

We need to create an object of the class, before use functions inside the class.

A class file can be put into two files:
*.h header file, where we put the function prototypes and variable declarations.
*.cpp c++ source file, where the function body resides.

When a team of programmers work together or you need to distribute your programs to public, sometimes you don’t need or want others to change the class bodies, you only need to distribute the header files.

The .cpp file should have the include and namespace statements.

Aclass::Afunction, the :: is called binary scope resolution operator, it means Afunction is a member of Aclass.

There are two ways of accessing members inside a class.

One way is to use dot separator as java do: object.function()

Another way is to get the reference or memory address of the object, then use arrow member selection operator to access the class members: pointer->function()

#include <iostream>

#include “Sally.h”

using namespace std;

int main() {

Sally sallyObject; //create Object from Class

Sally *sallyPointer = &sallyObject; //assign the address of objet sallyObject to a pointer variable sallyPointer

sallyObject.printCrap(); //access function using dot separator

sallyPointer->printCrap(); //access function using arrow member selection operator



Make a Comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: