Class and Object in C++

Advertisements

Access Specifiers in C++ Inheritance in C++

Class and Object in C++

Object is the physical as well as logical entity where as class is the only logical entity.

Class: Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.

A class in C++ contains, following properties;

  • Data Member
  • Method
  • Constructor
  • Block
  • Class and Interface

Object: Object is a instance of class, object has state and behaviors.

An Object in C++ has three characteristics:

  • State
  • Behavior
  • Identity

Syntax

class_name object_reference;

Example

Employee e;

State: Represents data (value) of an object.

Behavior: Represents the behavior (functionality) of an object such as deposit, withdraw etc.

Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

Class is also can be used to achieve user defined data types.

In real world many examples of object and class like dog, cat, and cow are belong to animal's class. Each object has state and behaviors. For example a dog has state:- color, name, height, age as well as behaviors:- barking, eating, and sleeping.

class in c++

Vehicle class

Car, bike, truck these all are belongs to vehicle class. These Objects have also different different states and behaviors. For Example car has state - color, name, model, speed, Mileage. as we;; as behaviors - distance travel

Vehicle class

Difference between Class and Object in C++

ClassObject
1Class is a container which collection of variables and methods.object is a instance of class
2No memory is allocated at the time of declarationSufficient memory space will be allocated for all the variables of class at the time of declaration.
3One class definition should exist only once in the program.For one class multiple objects can be created.

Syntax to declare a Class

Syntax

 class Class_Name
  {  
    data member;  
    method;  
  }  

Simple Example of Object and Class

In this example, we have created a Employee class that have two data members eid and ename. We are creating the object of the Employee class and printing the objects value.

Example

#include<iostream.h>
#include<conio.h>

class Employee
{
public:
int salary  // data member
void sal()
{
  cout<<"Enter salary: ";
  cin>>salary;
  cout<<"Salary: "<<salary;
}
};
 void main()
 {
  clrscr();
  Employee e; //creating an object of Employee
  e.sal();
  getch();
 }  

Output

Enter salary: 4500 
Salary: 4500
difference between class and object in c++
Access Specifiers in C++ Inheritance in C++

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements