1 Introduction to C++ Programing
Jyoti Pareek
Programming Language History
Programming is a process of writing solution to a problem in a computer language so that it can be executed by computer.
With increasing level of complexities of applications the methodology of programming have changed. Initially after the invention of computers the first programming method was- machine level programming. With few hundreds lines of code this approach worked well but as the complexity of programs increased a new language – Assembly Language was invented which uses symbolic representation of machine instructions. It was comparatively easy to write and maintain the code using assembly language. As the program size and complexity kept on increasing, higher level languages were introduced such as FORTAN, C , BASIC , COBOL . A higher level language is one which is closer to human language and thus further from machine language.
C language is general purpose , fast and widely used language. It is well suited for system level application as the code written in C runs faster.
But it is not suitable for large software development as the complexity increases and software become hard to maintain.
There was another language – Simula which had the object oriented features helpful in large software development but was not very efficient in term of time. C++ was inspired by both these languages, thus it contain the object oriented features as well as the efficiency of C .
C++ was created by Bjarne Stroustrup . Its development began in 1979 at Bell laboratories in New Jersey . The language was initially known as “C with classes” . In 1983 the name was changed to C++ , as the language was more than classes . C++ was extension to C language and has the backward compatibility with C.
There are so many programming languages available but C and C++ are one of the most widely used language in software industry. They are used for creating everything from Operating System to embedded systems, games, desktop application and so on.
C++ provides support for both procedural programming as well as object oriented programming. Let’s understand the difference between Procedure Oriented and Object Oriented programming paradigms.
Procedure Oriented Approach vs Object Oriented Approach
There are different approaches to build a solution to a specific problem known as Programming Paradigms. Procedure Oriented and Object Oriented are two major paradigms. The conventional approach (ex. FORTAN, COBOL and C) for solving a problem is Procedure Oriented Programming . In Procedure Oriented Programming the focus is on Procedures also known as Function or Routines. It gives the step by step approach to solve the problem .It is also known as top down approach – a big problem is broken into smaller segments (functions)
This approach is intuitive as we give a step by step solution to a problem. Procedure Oriented relies on Procedures and Data which are two separate concepts. Data move freely around these procedures thus there is no data security constraint. There is no control over the way the data is processed or modified. In Object Oriented approach these two concepts are bundled in to a single concept of Entity (object).
In Object Oriented every problem is perceived in terms of collection of Entities / Objects. Every Entity encapsulates the data and behavior in a single unit called Class. Here the data can be processed only in the way that has been defined by the class, this makes the application more maintainable.
Object oriented approach gives the power that the data can be hidden and cannot be accessed by outside world directly. Entities communicate with each other with the help of functions.
Let’s understand this with an example of an application for maintaining Students record. In Procedure Oriented programming the approach would be to break the system in smaller modules. We will think of the operation which needs to be performed in this application such as- Read student’s data, display student’s data , calculate the grade of a given student, calculate the percentage of a given student , generate a mark sheet of a given student.
These are some of the task which is required in the application. Now the second part is to decide what data are required for the application? Here the data would be required to store student detail thus we need to define a structure –Student having the attributes like – name , roll number, course , marks etc.
Let’s see the complete set of data and fucntions
struct Student
{
char name[50];
int roll;
char course[50];
int sem;
float marks[6];
};
Struct Student readStudent();
void displayStudent (struct Student s);
char calculateGrade(struct Student s);
float calculatePercentage(struct Student s);
void generateMarksheet(struct Student s);
Here the data part i.e. structure Student and the functions which operate on this Student structure are written separately. Student data is moving freely among these function therefore no data security is there.
Let’s see the same application design using Object Oriented programing approach. In Object Oriented approach we first find out the entities (objects) which take part into this application. Here the main entity is – Student (In actual System there can be more entities)
After identifying the entities now we need to find the attribute and behavior of these entities.
The Student entity can have the following attributes:
Student id, name, course, semester, marks etc.
And the behavior –
readStudent()
displayStudent()
calculateGrade()
calculatePercentage()
generateMarksheet()
In Object Oriented these attributes and behavior are combined into a single unit – Class.
class Student
{
private:
char name[50];
int roll;
char course[50];
int sem;
float marks[6];
public:
void readStudent();
void displayStudent ();
char calculateGrade();
float calculatePercentage();
void generateMarksheet();
} ;
Here the data part is kept private which means no outside function can directly access the attribute of this class.
Object Oriented Programming Concepts
Classes and Objects
A Class is a user defined data type and an Object is a variable of this Class.
For example suppose we want to store details of Book. There is no data type available for storing the details of book. We can define a C structure for this but a C structure defines only the attributes of an entity and not the behavior. So the functions which operate on this Book entity has to be defined separately .
Object Oriented approach allows us to create more powerful data type- Class, which gives more control over the data type. A class is an extension to C structure. The difference is that structure in C contains only attributes but a Class can contains attribute along with the behavior of the entity.
The class defines the blueprint of a real world entity. An Object is an instance of a class. If we need a real world entity then we need to instantiate object of a class.
Here is an example of a Time class:
Here the class contain the attributes of Time i.e. hours, minutes and seconds along with the methods to read time, display time, add two time entity and subtract time entity. We will see in the coming lectures that C++ class data type is as powerful as the standard data types.
Encapsulation and Data Hiding:
Encapsulation refers to combining both data and function that manipulate the data together. In C++ encapsulation is achieved by Class. As we have seen that a Class in C++ contains both data and functions which operate on data. This makes the data highly secured from direct access. In the above Time example the attribute of Time entity and the methods are encapsulated in the single Time class.
Encapsulation led to another important Object Oriented programming concept Data Hiding where a programmer can decide what part of the class should be visible (public ) and what must be hidden(private/protected) from outside world. In the Time class example the properties of class are kept “private” , which means these properties are available only to the functions of this class but hidden from outside world.
The advantage of this concept is that the data is safe; programmer cannot access the data directly. A non-member function cannot access an object’s private or protected data. (An exception to this is friend function which is covered in coming module)
Abstraction
The idea of abstraction is to have a higher level look at the task. Data Abstraction allows us to create our own data type (using class), where we can define the variables and operations on this new data type. (Known as Abstract Data Type) With data abstraction we can focus on what operation needs to be performed on the data without bothering about how these operations are implemented.
In the above Time Class example, if we want to find the difference of two Time objects then we just need to instantiate the Time objects and invoke the method subtractTime(Time t), but we don’t need to think how this difference is found . Abstraction is an excellent tool for managing complexity. Well designed abstraction can make complex and large size problem simpler and manageable.
Reusability:
Reusability refers to reusing the existing class. In C++ once a Class is defined, it can be reused in any application. for example once we define a Time Class as given in the example above, in any application if we need a Time object then we just need to instantiate this class.
C++ strongly supports reusability through Inheritance and STL (Standard Template Library). Inheritances allow a class to reuse data and methods of an existing class. STL contain built-in classes which can be used in any C++ application. STL contains the general purpose classes such as Stack, Vector, Queue etc.
Inheritance:
Real world objects do not exists in isolation. The basic idea of inheritance is defining a new object in terms of an existing object. The advantage of this feature is – code reusability and hierarchical relationship
For example if we define a Person class which contains the basic characteristics of a Person such as – name, dob , address etc. and methods such as readData() , DisplayData(), calculateAge() etc. Now if we want to define a Student class then either we can create a completely new class Student from scratch or we can use existing Person class and add the specific characteristics of Student. Thus inheritance imposes a hierarchical relationship where a child class inherits data as well as behavior (methods) from its parent class.
C++ strongly supports reusability through Inheritance and STL (Standard Template Library). Inheritance allow a class to reuse data and methods of an existing class. STL contain built-in classes which can be used in any C++ application . STL contains the general purpose classes such as Stack, Vector , Queue etc.
Polymorphism :
The meaning of polymorphism in general terms is – one name multiple forms. One function call may results into different behavior in different instances. Polymorphism helps in making user defined data type more powerful. We will discuss Polymorphism in successive lectures.
Summary:
- C++ is an extension of C language and has the backward compatibility with C.
- C++ model is not pure object oriented model. It gives power of both Procedure Oriented and Object Oriented Programming
- C++ Classes allows us to create more powerful data type.
- Elements of object oriented paradigm such as Encapsulation, Data hiding, Abstraction, Inheritance, Polymorphism etc. makes the language suitable for large software development.
you can view video on Introduction to C++ Programing |
Additional References
1) Stanley Lippmann, “C++ Primer”, Pearson Education.
2) Bjarne Stroustrup, “The C++ Programming Language”, Pearson Education.
3) Scott Mayer, “Effective C++”, Addison Wesley.
4) Bhushan Trivedi , Programming with ANSI C++, 2/e , Oxford University Press.
5) Yashavant P. Kanetkar “Let Us C++” , Bpb Publications.
6) Abhiram G. Ranade, “An Introduction to Programming through C++” , McGraw Hill
7) Ellis and B. Stroustrup “Annotated C++ Reference Manual”, http://www.stroustrup.com/arm.html
8) Herbert Schildt, “Complete Reference C++”, McGraw Hill Publications.
9) Ashok Kamthane, “Object Oriented Programming with ANSI and Turbo C++”, Pearson Education
10) E Balaguruswami, “Object Oriented Programming With C++”, Tata McGraw Hill
11) “C++ FAQs”, Pearson Education.