C++ Programming: An Introductory Tutorial

c++ programming
object oriented programming
classes
inheritance
tutorial

This page covers C++‘s object-oriented language features, contrasting them with C, and provides a quick guide to C++ programming language concepts. It covers classes, objects, constructors, destructors, friends, inheritance, and operator overloading. This tutorial focuses on features of C++ that make it an object-oriented programming language.

C++ generally offers better speed and code reusability compared to C. It also manages memory efficiently. However, for the same logic, C++ often requires more lines of code than C.

C++ Features Over C Language

Here are some features introduced in C++ that enhance C:

  • Variable Declaration: In C, variables must be declared at the beginning of a block before their use. In C++, variables can be declared in the middle of statements, at the point where they’re first needed.

  • Scope Resolution Operator: The double colon :: acts as the scope resolution operator. In C++, it accesses hidden global variables.

    int var1;
    void main(void) {
      int var1;
      var1 = 20;        // Assign value to local variable var1
      ::var1 = 60;      // Assign value to global variable var1
      printf("%d  %d", var1, ::var1); // Will print 20 and 60
    }
    
  • Function Overloading: C++ allows multiple functions with the same name, differentiated by their arguments. The compiler automatically calls the appropriate function based on the arguments provided.

    int addition(int, int);
    float addition(int, float);
    
  • Inline Functions: Primarily used when the code is short. C++ allows a function to be compiled inline, which helps overcome the drawbacks of macros.

    inline int SQUARE(int a) {
      return(a * a);
    }
    

    This creates multiple copies in the compiled code wherever it is called, rather than just one.

C++ Quick Guide

We’ll explore concepts like classes, objects, constructors, destructors, friends, inheritance, and operator overloading in this C++ tutorial.

Class and Object

A C++ class serves as a blueprint for creating objects. Any C++ class stores data and functions (called methods). These methods perform operations on the class data.

Constructor and Destructor

Constructors usually have the same name as the class itself. They initialize the data members of a class when an object is created. A constructor cannot return a value, so no return type is specified.

Destructors also share the class name but are preceded by a tilde (~). They are automatically called when an object is destroyed in a C++ program. Unlike constructors, destructors cannot accept any parameters/arguments.

Example: Class, Object, Constructor, and Destructor

class Class_name {
public:
  // Declaration of data members
public:
  // Declaration of member functions (methods)
};

Here’s an example using an Employee class and an object Emp1:

class Employee {
public:
  char 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");
  Emp1.Emp_ID = 2345; // Corrected assignment
  Emp1.Salary = 10455.50;
  Emp1.Disp_emp_detail(); // Display all above values we have initialized.
}

The Disp_emp_detail function can be defined outside the class using the scope resolution operator, but it must be declared within the 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 example:

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

Destructor example:

~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

These keywords control data access within a class.

  • Public: Any function can access public data members.

  • Private: Only the class itself and its friend classes/functions can access private data members.

  • Protected: The class, derived classes, and friend classes/functions can access protected data members.

new and delete Keywords

C++ allocates memory from the “free store” (heap). new is similar to malloc in C, and delete is similar to free.

Example:

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

Friend Class and Friend Functions

Private members of a class are typically only accessible by that class’s member functions. However, the “friend” concept allows other classes and functions to access these private members.

  • Friend Class Example:

    class a {
      friend class b; // Class b can access class a's private members
      // ...
    };
    
    class b {
      friend class a; // Class a can access class b's private members
      // ...
    };
    
  • Friend Function Example:

    friend return_type function_name(arguments);
    

    A friend function declaration is valid when declared within any class definition:

    class class_name {
      int value1;
      friend int accessing(class_name int); // Friend function declaration
    
    public:
      class_name(int x) {
        value1 = x;
      }
    };
    

    accessing function is a friend and can now access any private members of the class. The friend function prototype can also be declared within the class:

    friend return_type class_name::function_name(argument types);
    

Inheritance

Inheritance is the process where one or more classes are derived from a base class. Derived classes inherit the characteristics of the base class and can add their own unique features. C++ supports deriving classes from multiple base classes.

  • Inheritance Example:

    class derived_class1 : base_class {
      // Statements
    };
    
    class derived_class2 : derived_class1 {
      // Statements
    };
    

Operator Overloading

Traditional languages often restrict manipulation of user-defined data types. For example, in C, you can’t directly add two structures. In C++, operator overloading allows programmers to define custom behavior for existing operators when used with user-defined types.

The syntax is:

type operator op(parameters list)

where op is the operator to be overloaded, type is the return value, and parameters list are the arguments passed to the overloading function.

LabVIEW Cluster Basics: Grouping Mixed Data Types

LabVIEW Cluster Basics: Grouping Mixed Data Types

Learn how to use LabVIEW clusters to group mixed data types, reduce wire clutter, and simplify block diagrams. Explore cluster functions like Bundle, Unbundle, and reordering.

labview
cluster
data type
LabVIEW Programming Basics Tutorial

LabVIEW Programming Basics Tutorial

Learn the fundamentals of LabVIEW programming, including creating VIs, data types, syntax, and example implementations. Discover how to interface with RF instruments.

labview
programming
virtual instrumentation
MATLAB Programming Language Tutorial

MATLAB Programming Language Tutorial

This tutorial covers MATLAB basics, including syntax, environment, operators, control flow, matrices, functions, and a sample program.

matlab
programming
tutorial