What is the difference between C and C++?
C++ is next level programming language after C initally called as 'C with OOPs' and later changed to C++
C++ is also a programming language like C but it is based on Object Oriented Paradigm
It has all the capabilities of C Language with many additional features.
Some of the features of C++ are
First Feature
C++ do not require format specifiers unlike C. C++ provides built-in references under iostream.h header for input and output operations without need of any format specifier.
C++ is also a programming language like C but it is based on Object Oriented Paradigm
It has all the capabilities of C Language with many additional features.
Some of the features of C++ are
First Feature
C++ do not require format specifiers unlike C. C++ provides built-in references under iostream.h header for input and output operations without need of any format specifier.
- cin
- out
- cerr
Here cin refers to standard input which is keyboard, cout refers to the standard output which is monitor and cerr refers to standard error which generally again monitor.
These references require some operators to input and output the data
- << called as insertion operator and used with cout and cerr
- >> called as extraction operator and used with cin
These references are generally contained inside special container std called as namespace provided by C++. Before using them we need to import the std namespace with using keyword.
Example
WAP to input three numbers and show the biggest number.
Solution
#include<iostream>
using namespace std;
main()
{
int a,b,c,g;
cout << "Enter three numbers : ";
cin >> a >> b >> c;
g=a>b && a>c ? a : b>c ? b : c;
cout << "Biggest number is "<< g;
}
Output
Enter three numbers : 56 123 55
Biggest number is 123
Second Features
C++ allows to declare variables anywhere in the program wherever we need them, which is generally not allowed in C Language.
Example
WAP to input a number and show greatest of three numbers.
#include<iostream>
using namespace std;
main()
{
int a,b,c;
cout << "Enter three numbers : ";
cin >> a >> b >> c;
int g=a>b && a>c ? a : b>c ? b : c;
cout << "Biggest number is "<< g;
}
Output
Enter three numbers : 56 77 555
Biggest number is 555
Third Feature
C++ provides a concept of references which allows to work like pointers in C but without need of any pointer.
Syntax
datatype &referencename=variable;
Now referencename is a kind of nickname to the given variable where both are refering to same location and if we make any changes to referencevariable they will be done is the variable automatically, very similar to indirect memory referencing in C language but without using the pointers and dereferencing.
Example
WAP to create a function mutliresult() which takes a number as input and provides square, square root, cube and cube root of that number using poiners and references.
Solution 1: Using Pointers like C. Call by Address
#include <iostream>
#include <math.h>
using namespace std;
void multiresult(double n, double *sq,double *sroot,double *cb,double *croot)
{
*sq=n*n;
*sroot=sqrt(n);
*cb=n*n*n;
*croot=pow(n,1.0/3);
}
main()
{
double num,square,squareroot,cube,cuberoot;
cout<<"Enter a number : "; cin >> num;
multiresult(num,&square,&squareroot,&cube,&cuberoot);
cout <<"Square is "<<square << ", Cube is "<<cube << "\n";
cout <<"Square Root is "<<squareroot <<", Cube Root is "<<cuberoot;
}
Output
Enter a number : 7
Square is 49, Cube is 343
Square Root is 2.64575, Cube Root is 1.91293
Solution 2: Using references. No Pointers required. Call by reference.
#include <iostream>
#include <math.h>
using namespace std;
void multiresult(double n, double &sq,double &sroot,double &cb,double &croot)
{
sq=n*n;
sroot=sqrt(n);
cb=n*n*n;
croot=pow(n,1.0/3);
}
main()
{
double num,square,squareroot,cube,cuberoot;
cout<<"Enter a number : "; cin >> num;
multiresult(num,square,squareroot,cube,cuberoot);
cout <<"Square is "<<square << ", Cube is "<<cube << "\n";
cout <<"Square Root is "<<squareroot <<", Cube Root is "<<cuberoot;
}
Output
Enter a number : 7
Square is 49, Cube is 343
Square Root is 2.64575, Cube Root is 1.91293
Fourth Feature
C++ treats the structure and union as pure user defined data type and do not required to use the struct keyword while creating variables unlike C.
struct student
{
int rollno;
char name[51],course[21];
};
student s; //now student is working like other data types in in C
Fifth Feature
Structures of C++ can have data members and the functions both which is not allowed in C language
Example
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
int rollno;
char name[51],course[21];
void input()
{
cout <<"Roll No : "; cin >> rollno;
cout <<"Name : "; fflush(stdin);gets(name);
cout <<"Course : "; gets(course);
}
void show()
{
cout <<rollno<<", "<<name<<", "<<course;
}
};
main()
{
student s;
s.input();
s.show();
}
Output
Roll No : 123
Name : Vikas Kumar
Course : Advance Java
123, Vikas Kumar, Advance Java
Sixth Feature
C++ allows multiple functions with same name which is not allowed in C langauge.
Example
#include<iostream>
using namespace std;
void area(int side)
{
cout <<"Area of square is "<<side*side << endl;
}
void area(double radius)
{
cout <<"Area of circle is "<< 3.14*radius*radius << endl;
}
void area(double length, double width)
{
cout <<"Area of rectangle is " << length*width << endl;
}
main()
{
area(5);
area(5.0);
area(5.0,6.0);
}
Output
Area of square is 25
Area of circle is 78.5
Area of rectangle is 30
Note: All programs compiled and run with Dev C++ 5.11
C++ treats the structure and union as pure user defined data type and do not required to use the struct keyword while creating variables unlike C.
struct student
{
int rollno;
char name[51],course[21];
};
student s; //now student is working like other data types in in C
Fifth Feature
Structures of C++ can have data members and the functions both which is not allowed in C language
Example
#include<iostream>
#include<stdio.h>
using namespace std;
struct student
{
int rollno;
char name[51],course[21];
void input()
{
cout <<"Roll No : "; cin >> rollno;
cout <<"Name : "; fflush(stdin);gets(name);
cout <<"Course : "; gets(course);
}
void show()
{
cout <<rollno<<", "<<name<<", "<<course;
}
};
main()
{
student s;
s.input();
s.show();
}
Output
Roll No : 123
Name : Vikas Kumar
Course : Advance Java
123, Vikas Kumar, Advance Java
Sixth Feature
C++ allows multiple functions with same name which is not allowed in C langauge.
Example
#include<iostream>
using namespace std;
void area(int side)
{
cout <<"Area of square is "<<side*side << endl;
}
void area(double radius)
{
cout <<"Area of circle is "<< 3.14*radius*radius << endl;
}
void area(double length, double width)
{
cout <<"Area of rectangle is " << length*width << endl;
}
main()
{
area(5);
area(5.0);
area(5.0,6.0);
}
Output
Area of square is 25
Area of circle is 78.5
Area of rectangle is 30
Note: All programs compiled and run with Dev C++ 5.11
Comments
Post a Comment