Classes and Objects in C++
Classes and Objects in C++
Classes are considered to be backbone in Object Oriented Programming (OOP). OOP is a way of programming being used in modern computer programming.
Always all the modern computer languages support this way of programming and C++ has no exception. In fact the main difference between C and C++ is that C++ supports object oriented programming while C doesn’t.
There is no concept of object oriented programming without classes. A class is like the structure or blueprint of anything. It contains attributes and behaviors of the entity about which the class is created. For example the class of vehicle might have attributes like name, model, engine type, color etc. The behaviors might be ‘start vehicle’, ‘change gear’, ‘speed acceleration’, ‘stop vehicle’ and so forth.
The main difference between attributes and behaviors of a class is that attributes represent the properties while behaviors represent the processing. In technical terms, attributes are variables and behaviors are functions. In OOP, they are called data members and member functions respectively.
Anything which might have attributes, behaviors or both can be written in the form of a class. Class is the advanced form of structures being used and defined in C. A class works like a user-defined data type. Once a class is defined then it can be used to define variables (objects) of a class.
Objects
Objects are the instances of a class. A class itself doesn’t occupy any space in memory but if an object of a class is defined then it will need a space in memory to hold its data. Objects are actual representation of a class. For example if vehicle is a class then Mercedes is an object of vehicle class. Similarly sparrow, peacock, eagle can be called objects of bird class.
A class might have as many objects as required. Having said earlier, if we define a class then it acts like a data type and we can define one or more than one variables of this data type. Variables of a class are different from normal variables and are called objects of that class. Objects of a class can access all the variables and functions defined in that class.
Syntax of a class
class class_name
{
data_members;
member_functions();
}
Example
class Example
{
int a; //data member
void setA(int x) //member function
{
a=x;
}
void display() //member function
{
cout<<a;
}
}
void main()
{
Example e; //object of class
e.setA(99); //calling member function of the class
e.display();//calling member function of the class
}
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes