1.Class and Object in C++ OOPs

The fundamental ideas of object-oriented programming, or OOPs, are class and object, which are used to represent ideas and entities found in the real world.

Class is a blueprint for the object with detailed description. Class is a base of a Object Oriented Programming. It is a container that binds data and its associated functions together in a single unit.

Object is an instance of a class . class is a logical representation whereas object is a physical representation. when we create a class for that don’t get memory . when we create a instance of class i.e. object we get the memory to perform operations on it.

Eg. If we consider Student as a class then name, date of birth, marks of the student etc. will be the variables of the class student and
age calculation using date of birth , Grades calculation using marks will be the member functions of the class.

Class and Object in C++

Class :

  1. Class data is divided in
    (a)Private
    (b)Protected
    (c)Public
  2. Bydefault data is private
  3. Class allows the concept of inheritance
  4. We can able to make secure application

Syntax of class :

class className
{
  Access specifier:
    data members;
    member functions();
};

Object :

  1. Object is a instance of class
  2. Object is a class variable
  3. Object is a physical representation of class

Syntax of object :

className objectName;

Difference between the class and structure

CLASSSTRUCTURE
Classes are of reference typesStructures are of value types
All the reference types allocated on heap memoryAll the value types are allocated on stack memory
Allocation of large reference type is cheaper than allocation of large value typeAllocation and de-allocation is cheaper cheaper in value types than small reference types
class is generally used in large programsStructures used in small programs
class can be inherit from another classstructure is not allowed to inherit from another structure or class
The data members of a class can be protectedThe data members of structure can be protected
Member functions of a class can be virtual or abstractMember functions of structure cannot be virtual or abstract

C++ Code

#include<iostream>
using namespace std;
class student {
	int id;
	char name[20];
	public:
		void get()
		{
			cout<<" Enter student Id ";
			cin>>id;
			cout<<" Enter student Name ";
			cin>>name;
		}
		void put();
};

void student::put()
{
	cout<<" Printing Student Information "<<endl;//std::cout
	cout<<" Id : "<<id<<endl;
	cout&<<" Name : "<<name<<endl;
}

int main()
{
	student s;
	s.get();
	s.put();
	return 0;
}


You can download C++ for windows Click here

Zebronics War Gaming Keyboard and Mouse Combo,Gold Plated USB, Braided Cable,Multicolour LEDs/Gaming Mouse with breathing LEDs and 3200 DPI



Leave a Comment