Learn Programming with Fun

Category: Programming

Starting with C++ Programming

Hi, you opened this tutorial because you want to learn C++. This page will get you started and help you to write you first C++ program!

Firstly you need to get a C++ compiler, there are tons on Google. I will use Dev C++ as my compiler and CodeBlocks as my IDE. This is your program’s source.

#include

using namespace std;

int main()

{

cout<<“Hello World”;

}

Now that looks confusing. Well its not! Now the first line means you are calling a library to use, iostream is part of the standard library. It is really refered to as InputOutputStream. In other words it allows to to get input and throw an output.

Next is line 3. Each of the standard libraries has a namespace abreviated as std. We are telling C++ that we are using the standard namespace. Each program needs a main statement. It indicates that that is where the program starts. So we write int main. The int says we are going to return a value if needed.

Line 6 indicates that we are starting our program, while line 9 says that we are ending it. Line 7 says that we are going to write a message “Hello world!” and go to the next line. An easy way to remember this is cout stands for character out, in other words, words out.

The Thats it! You wrote your first C++ program! This is a quick tutorial and doesn’t teach, classes, templates, friends, variables etc, but if I get enough hits, I will gladly write about all of those!

Continue Reading

The Fundamentals of C++

Here are some aspects of C++ that should be known by every C++ software developer.

1. Struct, Union and Class

In C++ the struct is very similar to a class. The only difference is that the default members in struct are public and private in class.

struct str {

void add_elem ( int elem); /* public by default */

private:

int size;

int values[16];

};

class str {

void add_elem ( int elem); /* private by default */

private: /* already private */

int size;

int values[16];

};

In C++ union is very similar to a class.

Union has also all default members declared as public. Unions include also constructors and destructors.

2. Friend Class and Friend Function

A friend class has access to the private members defined within the other class.

It doesn’t inherit the other class, just has a privilege access.

A friend function doesn’t have a “this” pointer. Overloading binary operators using friend function will differ than the version using member functions by the number of arguments:

class complex {

private:

int real, imag;

public:

/* operator+ and operator- are both binary operations */

complex operator+(complex cmp);

friend complex operator- (complex cmp1, complex cmp2);

};

3. Inline Concept

Inline is treated by the compiler like a request, not a command. The compiler is free to not treat a function like inline if it considers that would be a negative decision.

4. Static Members

A static data member limits the existence of only one copy of that variable for all objects of the class. A declaration of static variable inside a class needs also to be forwarded by a definition for storage allocation.

class test {

static int x;/* declare x variable */

public:

……..

};

int test::x;/* define x variable */

A static function member doesn’t have the “this” pointer, may not be virtual and can not be declared const or volatile.

5. “This” Pointer

When a member function is called, an implicit argument that is a pointer to the invoking object is transmitted.

class complex {

private:

int real, imag;

public:

complex operator+(complex cmp);

};

/* this function member has only 1 argument even overload a binary operator because the second one is implicit transmitted using “this” pointer */

complex complex::operator +(complex cmp) {

complex temp;

temp.real = cmp.real + real; /* รณ temp.real = cmp.real + this.real;*/

temp.imag = cmp.imag + imag;

return temp;

};

6. Dynamic Allocation

“New” operator allocates memory in C++ and returns a pointer to the start of memory space. No size of memory need to be transmitted, like “malloc”.

“Delete” frees the memory allocated with new.

For memory allocation of a vector need to use [ ] operator for deallocation also:

Version 1:

int *p;

try {

p = new int[100];

} catch (bad_alloc amp;) { /* prepare for out of memory error */

assert(0);

}

….

delete [ ]p;

Version 2:

int *p

p = new (nothrow) int[100];

if (!p) {

assert(0);

}

7. Function Overloading

The function overloading can be achieved using the same name, but must be different the number or type of the arguments.

The return value difference is not enough for function overloading.

The function overloading doesn’t make difference between pointer and vector. The bellow functions are treated identically by the compiler.

int func (char *param);

int func (char param[ ]);

The following operators can not be overloaded: . :: .* ?

Comma operator can be overloaded:

complex complex:: operator, (complex cmp) {

complex temp;

temp.real = cmp.real;

temp.imag = cmp.imag;

return temp;

}

int main() {

complex cmp1(1,2), cmp2(2,3), cmp3(3,4);

cmp1 = (cmp1, cmp2, cmp3);

}

8. Copy Constructor – the special one!

Copy constructor is used when an object initialize another object (1), when an object is passed to a function(2) or when a temporary object is generated (3), like a return value.

9. Virtual Concept

A virtual function is a member that is declared within a base class and redefined by a derived class. Virtual functions are inherited.

class base {

public:

virtual void func();

};

class derived : public base {

public:

void func(); /* func() still remains virtual */

};

class superderived : public derived {

public:

void func(); /* func() still remains virtual */

};

A pure virtual function is not defined in the base class.

An abstract class contains at least one pure virtual function.

10. Const Function Members

A const member can not modify the object that invokes it.

class complex {

int real, imag;

public:

void change_func(int r, int i) const;

};

void complex::change_func(int r, int i) {

real = r; /* ERROR: can not modify the object */

imag = i;

}

11. Mutuable Data Members

A mutuable member can be changed by a const function member:

class complex {

mutuable int real, imag;

public:

void change_func(int r, int i) const;

};

void complex::change_func(int r, int i) {

real = r; /* OK: now can modify the object */

imag = i;

}

12. Volatile Function Members

A member declared as volatile causes this pointer to be treated as a volatile pointer.

class complex {

int real, imag;

public:

void change_func( int r, int i) volatile;

};

13. Const Data Members

Constant data members need to be initialized by constructor class and can not be assigned values after that.

class complex {

const int real, imag;

public:

complex( int r, int i): real(r), imag(i);

void change_func( int r, int i) { real += r; imag -= i; };

};

int main() {

complex comp(3, 4);

change_func( 5,6); /* ERROR */

}

14. The Explicit Constructor

A constructor specified as explicit will only be used when an initialization uses only the normal constructor syntax.

class mytest {

int real;

public:

explicit mytest( int r) { real = r; }

};

mytest test1(1); /* OK */

mytest test2 = 1; /* ERROR */

15. Member Functions

Functions that are declared within a class are called member functions. Members can access any element of the class of which they are part (public or private).

A non-static member can not have an initialize value.

A member can not be declared auto, extern or register.

Continue Reading