| Parameters | C | C++ |
|---|---|---|
| Programming type | It is a Procedural Oriented language | It is an Object-Oriented Programming language. |
| Approach | C language follows Top Down programming approach | C++ follow 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 programming language, a big program code is divided into small pieces which is called functions. | In C++ programming language, a big program code is divided into Objects and Classes. |
| Inline function | It does not allow inline function. | It supports 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. |
| Pointer | C supports only pointers. | C++ supports both pointers and references. |
| 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. |
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
A constructor in C++ is a special 'MEMBER FUNCTION' having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created.
C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. Operator overloading is a compile-time polymorphism. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.
Memory leaks occur when you allocate memory but forget to deallocate it. To prevent this, smart pointers are used it is a type of pointer that automatically deallocates memory when it is no longer needed. There are many different smart pointers available, such as std::unique_ptr, std::shared_ptr, and std::weak_ptr.
In C++, exception managing is executed using try , throw and catch block. The Try assertion lets in you to define code in an effort to be tested for mistakes upon execution. The throw keyword throws an exception when a problem is detected ; which we will use to make a deliberate blunders.
In function overloading, or more features with the same call but distinctive parameters are used in the equal program. The function is redefined with unique arguments or special arguments.
A pointer in C ++ is a variable that holds the reminiscence deal with of every other variable while reference is an alias for an already to be had variable. In case if reference is initialized to a variable then it can't be changed to refer to another variable like in pointer.
It is nothing but a base magnificence member function that you can redefine in a derived class to gain polymorphism. By using it , it's miles possible to declare the characteristic inside the base class the usage of the virtual key-word.
| Structure | Class |
|---|---|
| Members of the structure are public by default. | Members of the class are private by default. |
| When deriving a struct from a class/struct, default access specifiers for base class/struct are public. | When deriving a class, default access specifiers are private. |
Inheritance is a method by which one class inherited its properties from its parent class. Inheritance is a feature in which one new class is derived from the existing ones. The new class derived is termed a derived class, and the current class is termed a Parent or base class.
The new operator is used to request memory from the operating system, and it gives back a pointer which is allocated to memory . The delete operator should only be used for de-allocating the reminiscence allocated both the use of the brand new operator or for a NULL pointer
| Compile-time polymorphism | Run time polymorphism |
|---|---|
| In this method, we would come to know at compile time which method will be called. And the call is resolved by the compiler | In this method, we come to know at run time which method will be called. The call is not resolved by the compiler. |
| It provides fast execution because it is known at the compile time. | It provides slow execution compared to compile-time polymorphism because it is known at the run time. |
| It can be achieved by function overloading and operator overloading. | It can be achieved by virtual functions and pointers. |
In C++ there are the following access specifiers:
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the derived class.
Private: All data members and member functions are not accessible outside the class.
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. One of the important advantages of using an inline function is that it eliminates the function calling overhead of a traditional function
key idea in object-oriented programming (OOP) abstraction in C++ enables programmers to model classes based on real-world elements, hence simplifying complex systems. in order to simplify complicated systems and facilitate understanding and interaction with them, abstraction entails concentrating on key characteristics and behaviors while obscuring extraneous information.
If there are two or more functions with the same name defined in different libraries then how will the compiler know which one to refer to? Thus namespace came to the picture. A namespace defines the scope and differentiates functions, classes, variables etc. with the same name available in different libraries. The namespace starts with the keyword "namespace". The syntax for the same is as follows:
namespace namespace_name {
// code declarations
}
A template in C++ is used to pass data types as parameters. These make it easier and simpler to use classes and functions. e.g
template <typename T>
int fun (T a,T b)
{
return (a+b);
}
int main(){
cout<<fun<int>(11,22);
}
Pointers in C++ are a data type that store the memory address of another variable. By using pointer arithmetic concept we can manipulate address that is stored inside pointer also. There are different types of pointer apart from int, char and float, like dangling, null, bad and free pointer.
Destructors in c++ are special functions/methods that are used to remove memory allocation for objects. They are called usually when the scope of an object ends. eg. when a function ends you can call it a destructor.
Stl is the standard template library. It is a library that allows you to use a standard set of templates for things such as: Algorithms, functions, Iterators in place of actual code. e.g
queue<int> Q;
for(k=0;k<10;k++)
{
Q.push(k);
}
Type casting in C is used to change the one type of data data. They are of two types: Implicit Type Conversion: It is automatic. Explicit Type Conversion: It is user-defined.
A string is a sequence of characters. In C++, the string is a data type as well as a header file. This header file consists of powerful functions of string manipulation. A variable of string is declared as follows:
string str= "Hello"; // Include the string library #include <string> // Create a string variable string str= "Hello";
Tokens are the smallest building block or smallest unit of a C++ program. Compiler breaks a program into the smallest possible units and proceeds to the various stages of the compilation, which is called a token.
It User Defined Words whose meaning is unknown to the compiler. It is Case Sensitive and it should be alphanumeric starting with character like letters [A Z, a z] or underscore. It must not contain white space or a special character. Identifiers are names given to variables, functions, array, structure, union, etc. Examples Rate_of_interest , rate1, a123, _num, num_1, _num_1, PI, Sum etc.
There are Certain words are reserved whose meaning is already known to the compiler are called keywords. There are 95 reserved keyword in c++.
Constant is nothing but value given to the memory location or data stored. There are three type of constants- integer, float/real and character constants.
It is a type of data, which informs compiler about Memory required to store input or data, Type of input or data and Range of data. Data types are used extensively for declaring variables and functions of different types.
Operators Operate on operands and returns a value.
| OPERATOR | TYPE |
|---|---|
| ++,-- | Unary Operator |
| <,<=,>,>=,==,!= | Relational Operator |
| &&,||,! | Logical Operator |
| &,|,<<,>>,-,^ | Bitwise Operator |
| =,+=,-=,*=,%= | Assignment Operator |
| ?: | Ternary Operator |
The order of evaluation of operators is precedence. The order of operations in same precedence group is called associativity. The number of operands that are required for an operator to operate is called arity of the operator.
A program is nothing but set of instructions. There are few types of instruction as follows :
- DECLARATION INSTRUCTION - Used to declare type of variable
- ARITHMETIC INSTRUCTION - Used to perform arithmetic operations on constants and variables.
- CONTROL INSTRUCTION - Used to control the sequence of execution of various statements.
Console is nothing but a output window. So data read from that console window is nothing but data extraction so for that we use "cin" function with extraction operator like ">>" And for writing output on consol we use console output function that is "cout" by using "<<" insertion operator.
A character variable holds ASCII value rather than character itself. ASCII values are between 0 to 127 ASCII value of 'A' is 65 means if you assign 'A' to a character variable 65 is stored in that variable rather than character itself. Uppercase Alphabets 65 to 90 Lowercase Alphabets 97 to 122
It used to declare variable as a constant value. It is a fixed value which cannot be changed in a program. It becomes read only variable. It can be any of the basic data types.Syntax :
const datatype varname; / datatype const varname; Declared with keyword const const float PI = 3.14; const int CODE1 101;
If we declare the variable as volatile , then it serves as a warning to compiler that it should not optimize the code containing this variable. Its value can be changed in ways that cannot be determined by the compiler. volatile variable should be always read from memory and its optimization is not possible.
Some of the operators that cannot be overloaded are as follows:
- Dot operator- "."
- Scope resolution operator- "::"
- "sizeof" operator
- Pointer to member operator- ".*"
Runtime abnormal conditions that occur in the program are called exceptions. These are of 2 types:
- Synchronous
- Asynchronous
C++ has 3 specific keywords for handling these exceptions:
- try
- catch
- throw
A linear data structure which implements all the operations (push, pop) in LIFO (Last In First Out) order. Stack can be implemented using either arrays or linked list.The operations in Stack are
- Push: adding element to stack
- Pop: removing element from stack
- isEmpty: returns true if stack is empty
- Top: returns the top most element in stack
:: is called a scope resolution operator which is used to access global variables with the same name as of local variables, for defining functions outside the class, for accessing static variables, and for referring to a class inside of another class.
enum is abbreviation of Enumeration which assigns names to integer constant to make a program easy to read. Syntax for the same:
enum enum_name{const1, const2, ....... }
If the program does not have using namespace std; then when you write cout <<; you would have to put std::cout <<; same for other functions such as cin, endl etc.
| Reference | Pointers |
|---|---|
| Reference is used to refer to an existing variable in another name | Pointers are used to store the address of a variable |
| References cannot have a null value assigned | The pointer can have a null value assigned |
| A reference variable can be referenced by passing by the value | The pointer can be referenced but passed by reference |
| A reference must be initialized on the declaration | Pointers no need to be initialized on the declaration |
| A reference shares the same memory address with the original variable and takes up some space on the stack | Pointer has its own memory address and size on the stack |
- Mid-level programming language
- Portability
- C++ has the concept of inheritance
- Multi-paradigm programming language
- Memory Management
- C++ is a highly portable language
- Fast and Powerful
C++ is a standardized language and Visual C++ is a product that implements the standard of C++. One can write portable C++ programs using Visual C++, but one can also use Microsoft-only extensions which destroy portability but enhances your productivity.
std::flush synchronizes the stream buffer with its controlled output sequence.
A friend function in C++ is a function that is granted access to the private and protected members of a class. This means that it can read and modify these members, even though it is not a member of the class itself. Friend functions are often used to provide external functions that need to operate on the internal data of a class.
A sample code to see how to use vector in C++ is as follows:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector <string> vec_1;
vec_1.push_back("sample code");
vec_1.push_back("change example");
for(vector <string>::iterator i=vec_1.begin();i!=vec_1.end();++i)
cout<<*i;
return 0;
}
A pure virtual function is a type of virtual function which does not have implementation, but is only declared. It is declared by assigning 0 in declaration. Syntax for the same is as follows:
class Test
{
// Data members of class
public:
virtual void show() = 0;
/* Other members */
};
Inheritance in C++ is just like a child inherits some features and attributes from his parent similarly a class inherit attributes and methods from another class. The parent class is called base class and the child class is called derived class.
To prevent access to data directly, Encapsulation is the process that combines data variables and functions in a class. This is achieved by doing the following:
- Making all data variables private.
- Creating getter and setter functions for data variables.