C++ Concepts
C++ Concepts
1. Compiled Language
● C++ source program is translated into machine code using compilers.
● Executing compiled code is faster than interpreted code.
● Interpreted code translation happens at run time and one statement at a time, so
they are slower.
2. Object Oriented
It is object-oriented. Object oriented programming overcomes the shortcomings of
procedure oriented programming like global variables and no proper modeling of real
world objects.
Classes and Objects:
● Classes are blueprints or templates from which individual objects are created.
● Object is an instance of the class and represents a real-time entity.
● All objects have state and behavior.
● An object stores its State in member data and exposes its behavior through
member functions.
● Example: Class: Batsman Objects: Virat kohli, Rohit Sharma, Gill
● State: Matches, Innings, runs, Highest, Average, 100’s, 50’s
● Behavior: getMatches(), getRunsScored(), calculateAverage(), getAverage()
The fundamental concepts of object-oriented programming are:
(i) Abstraction:
● Abstraction is a way to manage the complexity of the problem. It does that by
focusing on essential things, and hiding details.
(ii) Encapsulation:
● It is a grouping mechanism. State and behavior is encapsulated as an Object.
● It promotes information hiding and modularity.
(iii) Inheritance:
● The derived class inherits all the capabilities of the base class but can add new
features and refinements of its own.
● It promotes reusability.
(iv) Polymorphism:
● It is the ability to present the same common interface for differing subtypes. Each
subtype behaves differently.
● Its benefits are extensibility and reusability.
3. Standard Library Algorithms
● The Standard Library Algorithms are based upon Standard Template Library (STL). It
provides several generic containers (collection of objects), functions or algorithms to use
and manipulate these containers.
● Some of the benefits are reduced development time (reusable code), the algorithms are
optimized and hence faster.
4. Statically Typed Language
A statically-typed language is a language (such as Java, C, or C++) where variable types
are known at compile time. Errors are detected earlier rather than at runtime. Runtime
errors are costlier. Performance optimization can be done.
5. Supports Exception Handling
Exception handling is a mechanism that separates code that detects and handles
exceptional circumstances from the rest of your program. It makes the code readable
and maintainable.
6. Memory Management
It supports dynamic memory management using new and delete operators. It does not
support automatic memory management like Java’s garbage collector, whereas in C++
the programmer has to manage the memory.
7. Structured Programming
Structured programming is a programming paradigm aimed at improving the clarity,
quality, and development time of a computer program by making extensive use of the
structured control flow constructs of selection (if/then/else) and repetition (while and for),
block structures, and subroutines.
8. Faster and High Performance
Since C++ is compiled, it is faster compared to other languages like Java, Python and
C# etc. It uses less memory footprint than Java, Python and C# etc.
9. Supports Generic Programming
It supports generic programming using templates. C++ provides templates to define the
same piece of code for multiple data types. The advantages are code reusability, and
avoidance of function overloading.
10. Supports Operator Overloading
Only a few languages like C++ support operator overloading. It allows users to use
standard operator symbols instead of different notations, making it easier to interpret.
11. Supports Lambda Expressions
C++ Lambda expressions are a convenient way to define an anonymous function object
or functor in C++. It can improve readability when writing callback functions.
class Rectangle{
private:
int length;
int breadth;
public:
void getDimensions() {
cout<<"Enter the length:";
cin>>length;
cout<<endl<<"Enter the breadth:";
cin>>breadth;
}
int calculateArea() {
int area = length*breadth;
return area;
}
void displayArea() {
cout<<"Area of rectangle="<<calculateArea()<<endl;
}
};
int main(){
Rectangle rectangle;
rectangle.getDimensions();
rectangle.calculateArea();
rectangle.displayArea();
}