Introduction to C++

C++ known as an extension to C language. C++ is a statically typed, free form, multiparadigm, compiled general-purpose language. C++ is an Object Oriented Programming language but is not purely Object Oriented.

History of C++

C++ was developed by Bjarne Stroustrup. In 1978 he started creating C++ and in 1979 he launches the first version of C++. Initially, there were some enhancements in C language & it was known as C with classes.

C++ Language

C was developed by Dennis Ritchie & in 1983 it was given a name as C++.

  • C++ is not the first object-oriented programming language. C++’s oops aspect is inspired by a computer simulation language called Simula67 (Simula67 is the world’s first object-oriented programming language).
  • Java is written in C++.
  • Major operating systems of modern times are written in C++
  • C++ is the world’s 4th most used programming language

Comparison between C and C++

  • C++ is a superset of C i.e all features of C (if else, loop, functions, Arrays, Strings ) can be used in C++.
  • C++ programs can use existing C software libraries.
  • C follows top down approach programming(i.e first we will create main () and decide the flow of the program and then we create detailed programs/functions)
  • While C++ follows bottom up programming approach(first we create detailed functionality and in last we’ll create main by assembling all these detailed functionalities)
  • C adopts procedure-oriented programming
  • C++ adopts object-oriented programming

What is object-oriented programming

OOPs is a programming approach which revolves around the below concepts:

  • Object(set of data & methods which can act on that data)
  • Class
  • Encapsulation— an act of combining properties and methods related to the same object
  • Data hiding
  • Abstraction
  • Polymorphism
  • Inheritance

Classes & Objects:

  • The class is a blueprint of an object
  • The class is a description of the object’s property set and a set of operations
  • Creating a class is as good as defining a new data type
  • Class is a means to achieve encapsulation
  • An object is a runtime entity(Class don’t get memory, object are allocated memory)
  • An object is an instance of a class

Example :

class box

{

int l,b,h;

void setDimension(int x, int y , int z){ }

Void showDimension(){  }

};

box b1;

Here box is a class(datatype) & b1 is a object. How much memory will be allocated to b1 will depend on how we have defined box.

Why Do We Need Object-Oriented Programming?

Procedural Languages

C, Pascal, FORTRAN, and similar languages are procedural languages. That is, each statement in the language tells the computer to do something: Get some input, add these numbers, divide by six, display that output.

Division into Functions

When programs become larger, a single list of instructions becomes unwieldy. Few programmers can comprehend a program of more than a few hundred statements unless it is broken down into smaller units. For this reason, the function was adopted as a way to make programs more comprehensible to their human creators. (The term function is used in C++ and C. In other languages the same concept may be referred to as a subroutine, a subprogram, or a procedure.) A procedural program is divided into functions,

Structured-Programming

The Object-Oriented Approach

The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions that operate on that data. Such a unit is called an object.

An object’s functions, called member functions in C++, typically provide the only way to access its data. If you want to read a data item in an object, you call a member function in the object. It will access the data and return the value to you. You can’t access the data directly. The data is hidden, so it is safe from accidental alteration. Data and its functions are said to be encapsulated into a single entity. Data encapsulation and data hiding are key terms in the description of object-oriented languages. If you want to modify the data in an object, you know exactly what functions interact with it: the member functions in the object. No other functions can access the data. This simplifies writing, debugging, and maintaining the program. A C++ program typically consists of a number of objects, which communicate with each other by calling one another’s member functions. What are called member functions in C++ are called methods in some other object-oriented (OO) languages (such as Smalltalk, one of the first OO languages). Also, data items are referred to as attributes or instance variables. Calling an object’s member function is referred to as sending a message to the object.

Characteristics of Object-Oriented Languages

  • Encapsulation: Act of combining properties and methods related to an object
  • Data hiding
  • Abstraction
  • Polymorphism
  • Inheritance

TERMS:

Objects

When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects.

Classes

In OOP we say that objects are members of classes.

Analogy: Almost all computer languages have built-in data types. For instance, a data type int, meaning integer, is predefined in C++. You can declare as many variables of type int as you need in your program:

int day;

int count;

int divisor;

int answer;

In a similar way, you can define many objects of the same class. A class serves as a plan, or blueprint. It specifies what data and what functions will be included in objects of that class. Defining the class doesn’t create any objects, just as the mere existence of data type int doesn’t create any variables.

A class is thus a description of a number of similar objects. This fits our non-technical understanding of the word class. Prince, Sting, and Madonna are members of the rock musician class. There is no one person called “rock musician,” but specific people with specific names are members of this class if they possess certain characteristics. An object is often called an “instance” of a class.

Inheritance

Classes divided into subclasses. Suppose the animal class is divided into mammals, amphibians, insects, birds, and so on. The vehicle class is divided into cars, trucks, buses, motorcycles, and so on.

The principle in this sort of division is that each subclass shares common characteristics with the class from which it’s derived. Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristics of vehicles. In addition to the characteristics shared with other members of the class, each subclass also has its own particular characteristics: Buses, for instance, have seats for many people, while trucks have space for hauling heavy loads.

Inheritance

An OOP class can become a parent of several subclasses. In C++ the original class is called the base class; other classes can be defined that share its characteristics, but add their own as well. These are called derived classes. Don’t confuse the relation of objects to classes, on the one hand, with the relation of a base class to derived classes, on the other. Objects, which exist in the computer’s memory, each embody the exact characteristics of their class, which serves as a template. Derived classes inherit some characteristics from their base class, but add new ones of their own.

Reusability

Once a class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called reusability. It is similar to the way a library of functions in a procedural language can be incorporated into different programs. However, in OOP, the concept of inheritance provides an important extension to the idea of reusability. A programmer can take an existing class and, without modifying it, add additional features and capabilities to it. This is done by deriving a new class from the existing one. The new class will inherit the capabilities of the old one, but is free to add new features of its own.

Creating New Data Types

One of the benefits of objects is that they give the programmer a convenient way to construct new data types. Suppose you work with two-dimensional positions (such as x and y coordinates, or latitude and longitude) in your program. You would like to express operations on these positional values with normal arithmetic operations, such as

position1 = position2 + origin

where the variables position1, position2, and origin each represent a pair of independent numerical quantities. By creating a class that incorporates these two values, and declaring position1, position2, and origin to be objects of this class, we can, in effect, create a new data type.

Polymorphism and Overloading

The = (equal) and + (plus) operators, used in the position arithmetic shown above, don’t act the same way they do in operations on built-in types such as int. The objects position1 and so on are not predefined in C++ but are programmer-defined objects of class Position. How do the = and + operators know how to operate on objects? The answer is that we can define new behaviors for these operators. These operations will be member functions of the Position class. Using operators or functions in different ways, depending on what they are operating on, is called polymorphism (one thing with several distinct forms). When an existing operator, such as + or =, is given the capability to operate on a new data type, it is said to be overloaded. Overloading is a kind of polymorphism; it is also an important feature of OOP.

C++ and C

C++ is derived from the C language. Strictly speaking, it is a superset of C: Almost every correct statement in C is also a correct statement in C++, although the reverse is not true. The most important elements added to C to create C++ concern classes, objects, and object-oriented programming. (C++ was originally called “C with classes.”) However, C++ has many other new features as well, including an improved approach to input/output (I/O) and a new way to write comments.

We are the best training institute, provide C++ training in Gurgaon along with many other programming languages.

Please follow and like us:
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *