A Step up to all facets of C Programming
VENKATESH BOPPANA ????
Engineer-2 (R&D), Micron Technology | MTech CSE'24 IIITB | GATE CS'22 | Mentor | Researcher | BTech IT'21 KU
April 28, 2022.
Hopefully, I assume you are familiar with the C programming language, which is general purpose, fast, portable and widely used. In this article we will be concerned with the essence of C++ programming language.
Let's have a glimpse of C?
Then, why C++ ? The hidden reason is Safety and convenience.?
In C, as it is procedural, there is no binding between data and functions, everything should be explicitly done to associate them. For large software systems, it is not a preferable way.
The right way is component wise building with added security and well structured. We have to shift to object-oriented programming for this.?
Notice real world items like refrigerators & televisions : Are they packaged, if so why??
Let's now look into this, those are packaged for convenient usage and to keep safe in both ways. Users are given only access to the control panel of the device and avoided to change internal voltages of circuit.
Similarly, our motive is to build a software component that is safe (less error prone) and convenient. We make use of struct gradually to build this software component for large software systems.
Main features of C++
Encapsulation: Data Members are binded to Member functions or methods. Any intended operation could be done using these functions. External access of data members is restricted unless specified or allowed.
Abstraction: Internal details of implementation are put away from the user. Only relevant information is accessed for behavioral fulfillment of method.
Classes and Objects: In a real-world scenario, an object is a distinguishable entity which has specific state, behavior and properties. These entities can be overlapped, generalized and even specialized.
For example consider the following code, which has fruit as a class and many fruits like apple, grapes can be created as an instance.
#include <bits/stdc++.h
using namespace std;
class fruit
{
????// members are by default private here
????/* data members */
public: //Access Specifier
????string name, color;
????double price;
?
????/* member functions */
????void print()
????{
????????cout << name << " " << color << " " << price <<???????
endl;
????}
};
int main()
{
????fruit obj;
????// object is created here
????obj.name="apple";
????obj.color="brown";
????obj.price=100;
????obj.print();
????return 0;
}
//Output:
//apple brown 100
>
Members of class are: Data members: same as Attributes Member functions: does some operation over data members.
?Actually, there is a lot more to unfold. All those details are out of scope of this article. For detailed content follow the Reference section which is at the end of this article.
The basic idea is,
?
Inheritance: As mentioned above objects can have overlapped properties. Classes can have similar properties and behavior with other classes. Additionally, it can have different members also.
?
Runtime Polymorphism
Virtual functions: Now the game is in different types of classes: Base(Parent) class and Derived(child) class. Generally C++ allows the base pointer to point to any object derived from the base class, it cannot directly access the members of the derived class. Thus virtual functions allow the base pointer to access the members of the derived class. Base class virtual function with no definition is known as 'Pure virtual function'.
领英推荐
?References: It is a variable that behaves as an alias for another variable and can be accessed from either of names, and acts as function formal parameters to support pass-by-reference. No need for extra memory for reference, where a pointer requires space for itself.
?Type Inference: Each time we access complex types, lengthy code at programmers end. Why can’t we leave it to the compiler? Yes, It’s the new feature, automatic deduction of type and assignment is achieved through auto, decltype keywords.
Compile time Polymorphism
Function and Operator Overloading: Ability to have two or more functions with the same name but may differ in type or number of arguments. Actual function calls are decided upon context and these functions have capability of performing various tasks.?
Type-safe free-store memory allocation (new/delete): Dynamic memory allocation through malloc(), calloc() and realloc() simply creates a block of memory and returns a void pointer. Advantages of new operator are:
Whereas, delete operator is used just to free the allocated memory and to add it to the free store.
?Protected members: Members of a class can be protected? using access specifiers. C++ has three types: Public, protected and private.
Static and const Member functions: The data members or methods that have class scope or not specific to any instance can be declared as static members.
‘const’ member functions helpful for const objects which cannot be modified. These objects however should be initialized during creation.
Templates: Templates used for generic programming. Declaring a generic template makes things easy and encourages code reusability.?
However, the compiler generates a function or class for a particular template and replaces generic type with passed type.
Exceptions: This is an additional feature added to later versions of C++, enabling better error handling.
Standard Template Library: It’s a set of classes for access to inbuilt data structures, containers, iterators and common algorithms. Includes utility library.
?Namespaces: It is a declarative region that provides a scope to the identifiers inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
Disclaimer:
This article is not meant for monetization and written just for community contribution.
Audience: Absolute C++ beginners or undergraduate students.
By?
BOPPANA VENKATESH
April 28, 2022.
References:
1.The Design and Evolution of C++, Bjarne Stroustrup
2.The C++ Programming Language, Bjarne Stroustrup