Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

C++ programming language Tutorial

Introduction

This page covers covers C++ object oriented language features over C and provide quick guide to C++ programming language concepts. It covers class,object,constructor, destructor,friend,inheritance and operator overloading.

This tutorial covers features of C++ programming language which is object oriented programming language. As far as speed is concern C++ is better than C. Code reusability is also better in C++. C++ manage its memory also efficiently. But for same piece of code for some logic, C++ will usually take more lines of code compare to C.

C++ features over C language

Following are some of the features introduced in C++ which makes C language a better C.

Variable declaration: In c language variables need to be declared before their use much at the beginning. In C++ language, variables can be declared in the middle of the statements, at the point where it need to be used for the first time.

Scope resolution Operator: The operator double colon(::) is used as Scope resolution Operator. In C++ it is used for hidden global variable as shown below. It is also used with classes which we will see later.

int var1;
void main(void)
{

int var1;
var1=20; //assign value to a local variable var1
::var1=60;//assign value to a global variable var1
printf("%d %d", var1,::var1); //will print 20 and 60

}

Overloading of functions: In C++ programming language it is possible to have same name for two or more functions. It is based on arguments automatically respective functions will be called by the compiler.
For Example,
int addition(int,int)
float addition(int,float)

Inline functions: This is mainly used when lines of code is less. C++ allows a function to be compiled inline. This helps overcome effects of macro.

inline int SQUARE(int a)
{
return(a*a);
}
This makes multiple copies in the compiled code instead of one wherever it is called.

C++ Quick Guide

Following are some of the concepts such as class,object,constructor, destructor,friend,inheritance,operator overloading that we will go through in this C++ tutorial.

Class and object

C++ class is basically a tool which help create objects. Any C++ class will store data and functions(called as methods). These class methods will perform operations on the class data.

Constructor,Destructor

Constructor will usually will have same name as the class itself. Constructor initializes the data members of a class when a object is created for the class. A constructor can not return a value, hence return type is not specified.

Destructor will usually will have same name as the class itself preceding with tilde sign (~). Destructor will be automatically called when any object is being destroyed in a c++ program. Unlike Constructor, you can not pass any parameters/arguments to a destructor.

Example of class,object,constructor,destructor

Class Class_name
{ public: //declaration of data members
public: //declaration of member functions(methods)

Following is the example of a class Employee and object Emp1.

class Employee
{
public: Emp_Name[15];
int Emp_ID;
Float Salary;
void Disp_emp_detail(void)
{
printf("Employee Name:%s", Emp_Name);
printf("Employee ID:%d", Emp_ID);
printf("Employee Salary:%f", Salary);
};
};

void main(void)
{
Employee Emp1; //object of class created here
strcpy(Emp1.Emp_Name,"James");
strcpy(Emp1.Emp_ID,2345);
Emp1.Salary=10455.50;
Emp1.Disp_emp_detail(); //Display all above values we have initialized.
}

The Disp_emp_detail function can be defined outside class as mentioned below(using scope resolution operator). In this case it has to be declared inside class.
public: void Disp_emp_detail(void); //declaration

void Employee:: Disp_emp_detail(void)
{
printf("Employee Name:%s", Emp_Name);
printf("Employee ID:%d", Emp_ID);
printf("Employee Salary:%f", Salary);
};

Constructor Employee is defined as below.

Employee:Employee(char *Name, int ID, float salary)
{
strcpy(Emp_Name, Name);
Emp_ID=ID;
Employee::Salary=salary;
}

Destructor can be declaraed and defined as below.
~Employee(void); //prototype declaration in class definition

Employee::~Employee(void)
{
printf("Deleting details of a particular Employee with ID= %d", Emp_ID);
}

Public,Private,Protected

Public,Private and protected are the three forms of declaraing data variables of a class in C++ for different functions as planned by the programmer as mentioned below.
public- Any function can access a data variable declared as public.
private-Only the class where it is declared and friends can access private data members of a class.
protected- Only the class where it is declared, derived classes of that class and friends of that class can access protected data members.

new and delete keyword

C++ allocates memory from a pool called free store. In C this pool is called as heap. new is similar to malloc of C programming. delete is similar to free of c programming.

Example:
Employee *Worker = new Worker("Samuel",2135,8945.25);
delete Worker;

Friend class and Friend Functions

As mentioned private members of a class can be accessed only by that class member methods(functions). But to exploit their access to other classes and to functions of the other classes frient concept is introduced. Hence private data members of the class can be accessed in friend class and friend functions of the class where private data members are declared.

Example: Friend Class

class a
{
friend b;
//few statements
}
Here class b member functions can access class a private members.
Class b
{
friend a;
}
Here class a member functions can access class b private members.

Example: Friend Function
friend return_type function_name(arguments);
A friend function declaration is legal when it is declared within any class definition as mentioned below.

class class_name
{
int value1;
friend int accessing(class_name int);
public: class_name(int x) { value1=x;}
};
accessing function is a friend function of class and hence can now access any private members of the class.

friend function prototype can be declared within the class definition itself as shown below.
friend return_type class_name::function_name(argument types);

Inheritance

The process by which one or more classes can be derived from a base class is called inheritance. Any class can be taken as base class to derive its children. Derived class will possess the characteristics of the base class. In addition they will have their own unique characteristics. C++ also supports derived classes from more than one base class.

Inheritance Example:

class derived_class1:base_class
{
//statements;
};

class derived_class2:derived_class1
{
//statements;
};

Operator Overloading

The traditional programming languages can not allow manipulation of user defined data types. For example in C , one can not add two structures. In C++ programmer can substitute user defined function for one of the available existing operators. The syntax for this is,
type operator op(parameters list),
Here op is the operator to be overloaded, type is the return value,parameters list are the arguments passed to the overloading function.


Share this page

Translate this page