C++ Lesson 01: Object-Oriented Programming
1.1 An Overiew of C++
What Is Object-Oriented Programming?
Object-oriented programming is a powerful way of approaching the job of programming. It took the best ideas of structured programming and combined them with several new concepts. The result was a different way of organizing a program. In the most general sense, a program can be organized in one of two ways: around its code (what is happening) or around its data (who is being affected). Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as "code acting on data." For example, a program written in a structured language such as C is defined by its functions, any of which may operate on any type of data used by the program.
Object-oriented programs work the other way around. They are organized around data, with the key principle being "data controlling access to code." In an object-oriented language, you define the data and the routines that are permitted to act on that data. Thus, a data type defines precisely what sort of operations can be applied to that data.
To support the principles of object-oriented programming, all OOP languages have three traits in common: encapsulation, polymorphism, and inheritance. Let’s examine each.
Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse. In an object-oriented language, code and data may be combined in such a way that a self-contained "black box" is created. When code and data are linked together in this fashion, an object is created. In other words, an object is a device that supports encapsulation.
Within an object, code, data, or both may be private to that object or public. Private code or data is known to and accessible only by another part of the object. That is, private code or data may not be accessed by a piece of the program that exists outside the object. When code or data is public, other parts of your program may access it even though it is defined within an object. Typically, the public parts of an object are used to provide a controlled interface to the private elements of the object.
For all intents and purposes, an object is a variable of a user-defined type. It may seem strange at first to think of an object, which links both code and data, as a variable. However, in object-oriented programming, this is precisely the case. When you define an object, you are implicitly creating a new data type.
Object-oriented programming languages support polymorphism, which is characterized by the phrase "one interface, multiple methods." In simple terms, polymorphism is the attribute that allows one interface to be used with a general class of actions. The specific action selected is determined by the exact nature of the situation. A real-world example of polymorphism is a thermostat. No matter what type of furnace your house has (gas, oil, electric, etc.), the thermostat works the same way. In this case, the thermostat (which is the interface) is the same no matter what type of furnace (method) you have. For example, if you want a 70-degree temperature, you set the thermostat to 70 degrees. It doesn’t matter what type of furnace actually provides the heat.
This same principle can also apply to programming. For example, you might have a program that defines three different types of stacks. One stack is used for integer values, one for character values, and one for floating-point values. Because of polymorphism, you can create three sets of functions called push() and pop()—one set for each type of data. The general concept (interface) is that of pushing and popping data onto and from a stack. The functions define the specific ways (methods) this is done for each type of data. When you push data on the stack, it is the type of data that will determine which specific version of the push( ) function will be called.
Polymorphism helps reduce complexity by allowing the same interface to be used to specify a general class of actions. It is the compiler’s job to select the specific action (i.e., method) as it applies to each situation. You, the programmer, don’t need to do this selection manually. You need only remember and utilize the general interface.
The first object-oriented programming languages were interpreters, so polymorphism was supported at run time. However, because C++ is a compiled language, polymorphism is supported at both run time and compile time.
Inheritance is the process by which one object can acquire the properties of another object. This is important because it supports the concept of classification. If you think about it, most knowledge is made manageable by hierarchical classifications. For example, a Red Delicious apple is part of the apple class, which in turn is part of the fruit class, which is under the larger food class. Without the use of classifications, each object would have to define all of its characteristics explicitly. Using classifications, an object need only define those qualities that make it unique within its class. It is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
1.2 Difference Between C and C++
C Is a Procedural Language
C is procedure-oriented programming (POP). POP consists of a set of instructions to be followed and divides these instructions into smaller parts known as functions in order for the computer to perform.
Features of POP :
- Mainly focused on writing the algorithms.
- The most function uses Global data for sharing which are accessed freely from function to function in the system.
- POP follows the top-down approach in program design.
- It does not have data hiding options.
- Functions transform data from one form to another.
- Data can be moved openly from one function to another around the system.
- Sub-division of a large program into smaller programs called functions.
- The overloading process is not applicable in POP.
C programs follow a procedure of steps written in it, called functions. It follows a top-down approach i.e. much importance is given to the flow of the program rather than on data on which functions operate.
A procedure in C is also called a function or even a subroutine. In "C", all procedures/functions/subroutines return a value even if it is not assigned and/or used.
- C language uses the set of instructions to inform/guide computers on what to do step by step.
- It depends on the procedures, more specific routines, or subroutines.
- As it follows the procedures hence it adopts a top-down approach.
- Apart from other languages like C++, C languages are very much focused on the procedure that relates to the data.
- C language is much focused on the data hence on functions.
- It is also well known as a structured programming language.
On the other hand, Java/C++/Python are object-oriented languages. They have a bottom-up approach. They revolve around classes and objects and follow principles of inheritance, abstraction, encapsulation, and polymorphism. C language does not have any such functionality. It follows whatever steps are written in the function.
Comparison list of C and C++
|Basis of Distinction||C||C++|
|Programming type||It is a Procedural Oriented language.||It is an Object-Oriented Programming language.|
|Approach||C language follows a Top-Down programming approach||C++ follows a Bottom-Up programming approach.|
|File extension||The file extension of a C program is .c.||The file extension of a c+ + program language is .cpp.|
|Program division||In C, a big program code is divided into small pieces which are called functions.||In C++, a big program code is divided into Objects and Classes.|
|Structure||Structure in C does not provide the feature of the function declaration.||Structure in C++ provides the feature of declaring a function as a member function of the structure.|
|Inline function||It does not allow inline function.||It supports the inline function|
|Standard I/O operations||In C, scan() and printf() are used for the standard input and output.||In C++, cin >> and cout << are given for standard input and output operations.|
|Data Security||In C language the data is not secured.||Data is secure, so it can not be accessed by external functions. (Using Encapsulation concept of OOPs).|
|Ease of Coding||In C language, you must tell the program to do everything. Moreover, this language will let you do almost anything.||C++ is an extension language of C. It allows for highly controlled object-oriented code.|
|Pointer||C supports only Pointers.||C++ supports both pointers and references.|
|Variable||In C, the variable should be defined at the beginning of the program.||C++ allows you to declare variables anywhere in the function.|
|Point of Focus||C focuses on the steps or procedures that are followed to solve a problem.||C++ emphasizes the objects and not the steps or procedures. It has a higher abstraction level.|
|Function Overloading||C does not allow you to use function overloading.||C++ allows you to use function overloading.|
|Exception Handling||C does not support Exception Handling. However, it can be performed using some workarounds.||C++ supports Exception handling. Moreover, this operation can be performed using try and catch block.|
|Functions||Does not allows functions with default arrangements.||Allow functions with default arrangements.|
|Namespace||It is absent in the C language.||It is present in the C++ language.|
|Relationship||C is a subset of C++. It cannot run C++ code.||C++ is a superset of C. C++ can run most of C code while C cannot run C++ code.|
|Driven by||Function-driven language||Object-driven language|
|Focus||Focuses on method or process instead of data.||Focuses on data instead of method or procedure.|
|Encapsulation||Does not support encapsulation. As Data and functions are separate and free entities.||Supports encapsulation. Data and functions are encapsulated together as an object.|
|Memory management||C provide malloc() and calloc() functions for dynamic memory allocation.||C++ provides a new operator for this purpose.|
|Global Variables||Allows Multiple Declarations of global variables.||Multiple Declarations of global variables are not allowed.|
|Inheritance||Inheritance is not supported C.||Inheritance is possible in C++ language.|
|Virtual function||The concept of virtual Functions is present in C.||The concept of virtual Function is not used in C++.|
|Polymorphism||In C. Polymorphism is not possible.||The concept of polymorphism is used in C++. Polymorphism is one of the most Important Features of OOPS.|
- C is a Procedural-Oriented Programming language, whereas C++ is an Object-Oriented Programming language.
- In C, a big program code is divided into small pieces which are called functions. In C++, a big program code is divided into Objects and Classes.
- C supports only pointers whereas C++ supports both pointers and references.
- C does not allow you to use function overloading whereas C++ allows you to use function overloading.
- C language follows the Top-Down programming approach whereas C++ follows a Bottom-Up programming approach.
- C scan() and print() are used for the standard input and output while in C++, cin>> and cout<< are given for standard input and output operations.