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