Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Class Scope and Accessing Class Members

Data Structures using C ++

Data Structures using C PlusPlus: Data Abstraction and Overloading: Class Scope and Accessing Class Members

Class Scope and Accessing Class Members

• Each class is a collection of data and functions that manipulate the data.

• Placing the data and functions together into a single entity is the central idea in object oriented programming.

• The nouns in the system specification help the C++ programmer to determine the set of classes. The objects for these classes are created. These objects work together to implement the system.

• Classes in C++ is the natural evolution of C notion of struct.

For example:

class rectangle

{

    private:

       int len,br;

   public:

       void get_data();

       void area();

       mioold0 genforeg

       void print_data();

};

 

• Rules for declaring class name ‒

1. The class name must begin with the letters, it may be followed by letters, digits or underscore.

2. The name of the class must not be same as keyword or reserved word.

3. The class name must not contain any special character such_as_~,!,@,#,$, %,^,&,*,(,),{,},[,],+,‒, |, /, \

 

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.

 

Difference between Structure and Class


Structure

1. By default the members of structure are are public.

2. The structure can not be inherited.

3. The structures do not require constructors.

4. A structure contains only data members.

Class

1. By default the members of class private.

2. The class can be inherited.

3. The classes require constructors for initializing the objects.

4. A class contains the data as well as the function members.

 

1. Creating 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 object xyz

 

2. Accessing Class Members

There are two types of Class members –

i) Data members and

ii) Member functions

For accessing them the object of that class is created. The syntax of accessing class members is ‒

object_ name.class_member;

Following is a simple C++ program which illustrates the idea of object instantiation and interaction.

#include<iostream>

using namespace std;

class Test

{

    private:

       int a,b,c,

    public:

       int Addition(int a, int b)

       {

              c=a+b;

              return c;

       }

       void Display()

       { cout << "The sum is:" << c<< "\n";}

};

void main()

{

       Test obj:                // instantiation of object

       obj.Addition(10,20);

       obj.Display();

}

Output

The sum is: 30

Program Explanation

After creating a class we can create the objects belonging to that class. This process is called instantiation of the object. The object can be created as a variable of type class. We can create more than one objects belonging to the same class. In above code we have created an object named obj. The creation is as follows ‒

Test obj;

There are two methods belonging to the class Test ‒ Addition and Display. Using object obj we can pass the values 10 and 20 to the method Addition. These values are then assigned to the variables a and b. The addition of these numbers is performed. The result is then stored in variable c and this variable is returned. The Display function makes use of this variable to display the result of addition of two numbers.

Example: 1

Explain the concept of passing an array of objects as an argument

Solution: We can pass an array of objects as an argument to the function. Following is a simple program that illustrates this idea.

#include<iostream>

using namespace std;

class Test

{

       int a;

     public:

       void setvalue(Test arr[10]);

       void display(Test arr[10]);

};

void Test::setvalue(Test a[10])

{

       for(int i=0; i<5; i++)

       {

              a[i].a=i;

       }

}

void Test::display(Test a[10])

{

       for(int i=0;i<5;i++)

       {

              cout<<" "<<a[i].a;

       }

}

void main()

{

       Test obj1[10];

       Test obj2;

       cout<<"\n Setting the values";

       obj2.setvalue(obj1);

       cout<<"\n The values are ";

       obj2.display(obj1);

}

Output

Setting the values

The values are 0 1 2 3 4

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ - Class Scope and Accessing Class Members


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 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


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation