class name{
// some data
// some functions
};
C | C++ |
* C language was developed by Dennis Ritchie. | * C++ language was developed by Bjarne Stroustrup. |
* C is a structured programming language. | * C++ supports both structural and object-oriented programming language. |
* C is a subset of C++. | * C++ is a superset of C. |
* In C language, data and functions are the free entities. | * In the C++ language, both data and functions are encapsulated together in the form of a project. |
* C does not support the data hiding. Therefore, the data can be used by the outside world. | * C++ supports data hiding. Therefore, the data cannot be accessed by the outside world. |
* Namespace features are not present in C | * Namespace is used by C++, which avoids name collisions. |
* Functions can not be defined inside structures. | * Functions can be defined inside structures. |
* calloc() and malloc() functions are used for memory allocation and free() function is used for memory deallocation. | * new operator is used for memory allocation and deletes operator is used for memory deallocation. |
* In C, scanf() and printf() are mainly used for input/output. | * C++ mainly uses stream cin and cout to perform input and output operations. |
C++
program with runtime polymorphism (use of virtual functions) :bp->show()
calls show()
function of Derived class, because bp points to an object of Derived class.#include<iostream>
using namespace std;
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base {
public:
void show() { cout<<"In Derived \n"; }
};
int main(void) {
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}​
#include <iostream>
#include <string>
using namespace std;
int main() {
string education = "Free Time Learning";
cout << education << "\n";
cout << &education << "\n";
return 0;
}​
Output : 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. |
class Student
{
//data members;
//Member functions
}
Student s = new Student();
class derived-class: access-specifier base-class
C++
are simply a piece of source code ignored by the compiler. They are only helpful for a programmer to add a description or additional information about their source code.//single-line comment
/* block comment */
“//”
. In the second type, the compiler discards everything between “/*”
and “*/”
.#include <iostream>
using namespace std;
class Encapsulation
{
private:
// data hidden from outside world
int x;
public:
// function to set value of
// variable x
void set(int a)
{
x =a;
}
// function to return value of
// variable x
int get()
{
return x;
}
};
// main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
Output : 5
Inline return-type function-name(parameters)
{
// Function code goes here
}
int Result;
char c;
int a,b,c;
Result = 10;
C = ‘A’;
#include <iostream.h>
Int globalResult=0; //global variable
int main()
{
Int localVar = 10; //local variable.
…..
}
#include <iostream>
using namespace std;
int main() {
cout << "Hello C++" <<endl;
return 0;
}
Output : Hello C++
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main () {
// Local variable declaration:
int g = 10;
cout << g; // Local
cout << ::g; // Global
return 0;
}​
return_type class_name : : operator op(argument_list)
{
// body of the function.
} ​
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
namespace namespace_name {
// code declarations
}
class X{
public:
// Constructor for class X
X();
// Destructor for class X
~X();
};
sum(int num1, int num2)
sum(int num1, int num2, int num3)
sum(int num1, double num2)
Function overloading example program :
#include <iostream>
using namespace std;
class Addition {
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
int main(void) {
Addition obj;
cout<<obj.sum(27, 18)<<endl;
cout<<obj.sum(81, 108, 9);
return 0;
}
Output :
45
198
string str= "Hello";
// Include the string library
#include <string>
// Create a string variable
string str= "Hello";​
Class_name(const class_name &old_object);
class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
Output :
x = 20
ref = 30
Reference
|
Pointer |
Reference behaves like an alias for an existing variable, i.e., it is a temporary variable. | The pointer is a variable which stores the address of a variable. |
Reference variable does not require any indirection operator to access the value. A reference variable can be used directly to access the value. | Pointer variable requires an indirection operator to access the value of a variable. |
Once the reference variable is assigned, then it cannot be reassigned with different address values. | The pointer variable is an independent variable means that it can be reassigned to point to different objects. |
A null value cannot be assigned to the reference variable. | A null value can be assigned to the reference variable. |
It is necessary to initialize the variable at the time of declaration. | It is not necessary to initialize the variable at the time of declaration. |
new()
is a preprocessor while malloc()
is a function.malloc()
you have to use sizeof()
.malloc()
gives random value in the newly allotted memory location.new()
operator allocates the memory and calls the constructor for the object initialization and malloc()
function allocates the memory but does not call the constructor for the object initialization.new()
operator is faster than the malloc()
function as operator is faster than the function. terminate()
function is invoked during Stack Unwinding on a destructor for a unhandled exception.inline return_type function_name( arguments )
{
// BODY OF THE FUNCTION
}
sample operator + ( sample a, sample b = sample (2, 3.5f ) ){
}
void main( )
{
sample s1, s2, s3 ;
s3 = s1 + ; // error
}​
// CPP program to illustrate
// Run Time Type Identification
#include<iostream>
using namespace std;
class B { };
class D: public B {};
int main()
{
B *b = new D;
D *d = dynamic_cast<D*>(b);
if(d != NULL)
cout<<"works";
else
cout<<"cannot cast B* to D*";
getchar();
return 0;
}
// CPP program to illustrate
// Run Time Type Identification
#include<iostream>
using namespace std;
class B { virtual void fun() {} };
class D: public B { };
int main()
{
B *b = new D;
D *d = dynamic_cast<D*>(b);
if(d != NULL)
cout << "works";
else
cout << "cannot cast B* to D*";
getchar();
return 0;
}
(==)
and assignment operator (=)
are two completely different operators.(==)
is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.(=)
is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.#include <iostream>
using namespace std;
int main() {
int a, b;
a = 4;
b = 5;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;
// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}
#include <iostream>
int main()
{
int row = 2, col = 2;
int* a = new int[row * col];
int i, j, count = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
*(a+ i*col + j) = count++;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
printf("%d ", *(a + i*col + j));
delete[ ] a;
return 0;
}
Output :
0 1 2 3
#include<iostream>
using namespace std;
int main()
{
int a= 4, b= 5;
bool c, d;
c= a== b; // false
c= a< b; // true
cout <<b1;
cout << b2 ;
return 0;
}
(>>>)
whereas C++
does not.#include <iostream>
using namespace std;
int main()
{
string s_1, s_2, fin;
cout << "Enter string";
getline (cin, s_1);
cout << "Enter string ";
getline (cin, s_2);
fin= s_1 + s_2;
cout << fin;
char str1[50], str2[50], fin[100];
cout << "Enter string";
cin.getline(str1, 50);
cout << "Enter string";
cin.getline(str2, 50);
strcat(str1, str2);
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2;
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int a=5.42;
int x = abs(a);
cout << x;
return 0;
}
Output : 5
#include<iostream>
#include<cstring>
using namespace std;
main() {
string s = "Free Time Learn";
char arr[] = "Free Time Learn";
cout << s.length();
cout << s.size();
cout <<strlen(arr);
char *c = arr;
int count = 0;
while(*c != '\0'){
count++;
c++;
}
cout << count;
count = 0;
for(int i = 0; arr[i] != '\0'; i++){
count++;
}
cout << count;
}
Output :
1515151515
class className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
A::b(int, long)
const is mangled as ‘b__C3Ail
’.A:: A(int, long)
const is mangled as ‘C3Ail
’.