C++

Creator of

Adobe Photoshop | Spotify | YouTube | Amazon | Windows OS

MS Office | Google | Firefox | Bloomberg



C++ Introduction

C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language.
C++ gives programmers a high level of control over system resources and memory.
The language was updated 4 major times in 2011, 2014, 2017, and 2020 to C++11, C++14, C++17, C++20.

Why Learn C++

C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C, C# and Java, it makes it easy for programmers to switch to C++ or vice versa.

Simple Program

#include <iostream>

int main() {
   cout << "SayabiDevs Full Stack Development Trainee Programme";
   return 0;
}


Comments

Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.

Single Line Comments

C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C, C# and Java, it makes it easy for programmers to switch to C++ or vice versa.

Example

// This is a single line comment
// This is another single line comment
cout << "Hello World";


User Input

Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.

Single Line Comments

As studied earlier, cin is used to output text to console
Similarly, cin is used with >> to take input from the user

Example

#include iostream

int main() {
  int num;
  cout << "Enter a number";
  cin >> num;
  cout << "You entered" << num;
  return 0;
}


Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Creating Function

To create a function, specify the name of the function, followed by parentheses ():

Example

void func() {
  cout << "This is a function";
}

Calling a function

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are called.

Example

void func() {
  cout << "This is a function";
}

int main() {
  func();
  return 0;
}


Object Oriented Programming (OOP)

OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

Class

A class is a template definition of the method(s) and variable(s) in a particular kind of object

Object

An Object is a specific instance of a class
It contains real values instead of variables

Creating a Class

class keyword is used to create a new class

Example

class Student {
  public:
    int rollNo;
    string name;
};



Constructors

A constructor in C++ is a special method that is automatically called when an object of a class is created.
To create a constructor, use the same name as the class, followed by parentheses ()

Example

class Student {
  public:
    int rollNo;
    string name;
};

Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.

Accessing Private Member(s)

To access a private attribute, use public "get" and "set" methods

Example

class Student {
  private:
    int rollNo;
  public:
    Student() {
      cout << "Student Contstructor" ;
    }

    void setRollNo(int r) {
      rollNo = r;
    }
};

int main() {
  Student obj;
  return 0;
}



Inheritance

In C++, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class (parent):

Example

// Base class 
class Vehicle {
    public:
        string brand = "Ford";
        void honk() {
        cout << "Tuut, tuut! \n" ;
        }
};

// Derived class 
class Car: public Vehicle {
    public:
        string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}
                    


Why Inheritance?

Inheritance is useful for code reusability: reuse attributes and methods of an existing class when you create a new class


Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance

Example

// Base class 
class Vehicle {
    public:
        string brand = "Ford";
        void print() {
        cout << "This is a Vehicle \n" ;
        }
};
                        
// Derived class 
class Car: public Vehicle {
    public:
        void print() {
            cout << "This is a Car \n" ;
        }
};

// Derived class 
class Bike: public Vehicle {
    public:
        void print() {
            cout << "This is a Bike \n" ;
        }
};
                        
int main() {
    Car myCar;
    Bike myBike
    myCar.print();
    myBike.print();
    return 0;
}
                                            


Why Polymorphism?

Polymorphism is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.


About Us

Avatar

Sufiyaan Usmani

usmanisufiyaan@gmail.com

Avatar

Zohaib Ahmed

zhbah234@gmail.com

Avatar

Muhammad Usama

mu155225@gmail.com