Object Oriented Programming: Chapter 2: Classes and Objects

Objects

C++ Programming | Object Oriented Programming

The object is an instance of a class. Hence object can be created using the class name. The object interacts with the help of procedures or the methods defined within the class.

Objects

• The object is an instance of a class. Hence object can be created using the class name. The object interacts with the help of procedures or the methods defined within the class.

• In order to instantiate an object we need to declare an object along with the class name.

For example ‒

int a; //an instance of type integer

double marks;//an instance of type double

Student xyz;//an instance of Student is xyz

Difference between Class and Object

Following are some differences between the class and the object –


Class

1.For a single class there can be any number of objects. For example‒ If we define the class as River then Ganga, Yamuna, Narmada can be the objects of the class River.

2.The scope of the class is persistent throughout the program.

3.The class can not be initialized with some property values.

4.A class has unique name.

Object

1.There are many objects that can be created from one class. These objects make use of the methods and attributes defined by the belonging class.

2.The objects can be created and destroyed as per the requirements.

3.We can assign some property values to the objects.

4.Various objects having different names can be created for the same class.

 

Example :1

 Write a C++ program to find out the sum and average of three numbers using nesting of member function.

Solution :

#include<iostream>

using namespace std;

class SumAvg

{

       int a,b,c;

        long total,avg;

        public:

              void calculate();

              void getnumber();

};

void SumAvg::getnumber()

{

       cout<<"\nEnter three numbers";

       cout<<"\n Enter a: ";

       cin>>a;

       cout<<"\n Enter b:

       cin>>b;

       cout<<"\n Enter c: ";

       cin>>c;

}

void SumAvg::calculate()

{

       getnumber();//nested function call

       total=0;

       cout<<"a = "<<a<<" b = "<<b<<" c = "<<c;

       total=a+b+c;

       avg=total/3;

       cout<<"\nSum: "<<total;

       cout<<"\b Average: "<<avg;

}

int main()

{

       SumAvg obj;

       obj.calculate();

       return 0;

}

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Objects


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation