Accessing a Class Members Assignment Help

Assignment Help: >> Classes and Objects - Accessing a Class Members

Accessing a Class Members

At once an object of a class has been created; there must be a provision to access its members. This is achieved through using the member access operator, dot (.). The syntax for accessing members (data and functions) of a class is display below

Objectname. datamember

Objectname . functionname(actual arguments)

A pair of parentheses is to be added following the function name if a member to be accessed is a function. The given statements access member functions of the object s1 that is an instance of the student class:

S1.setdata(10,"Rajkumar");

S1.outdata();

The program student.cpp describes the declaration of the class student along with the operations on its objects.

// student.cpp: member functions described inside the body of the student class

#include <iostream.h>

#include<string.h>

class student

{

private:

int roll_no;     // roll number

char name[20];// name of a student

public :

// initializing data members

void setdata(int roll_no_in,char *name_in)

{

roll_no = roll_no_in;

strcpy(name,name_in);

}

// show data members on to the console screen

void outdata()

{

cout << "Roll No = " << roll_no <<endl;

cout << "Name =" << name << endl;

}

};

void main()

{

student s1;     // 1st object / variable of class student

student s2;     // 2nd object / variable of class student

s1.setdata(1,"Tejaswi");      // object s1 calls member setdata()

s2.setdata(10,"Rajkumar");  // object s2 calls member setdata()

cout << "Student details ..." << endl;

s1.outdata();  // object s1 calls member function outdata()

s2.outdata();  // object s2 calls member function outdata()

}

Run

Student details ... Roll No = 1

Name = Tejaswi

Roll No = 10

Name = Rajkumar

Within conventional programming languages, a function is invoked on a piece of data (function-driven communication), therefore in an object-oriented programming language (OOPL), a message is sent to an object (message -driven programming) that is conventional programming is based on function abstraction therefore; object oriented programming is based on data abstraction.

The object accessing its class members resembles a client-server model. A client searches a service thus; a server gives services requested through a client. Within the example, the class student resembles a server therefore, the objects of the class stduent resembles clients. They make calls to the server through sending messages. Within the statements

S2.setdata(10,"Rajkumar");            // object s2 calls member function setdata

The object s2 sends the message setdata to the server within the parameters 10 and Rajkumar. As a server, the member function setdata() of the class student performs the operation of setting the data members according to the messages sent to it. As same, the statements

S2.outdata()

Could be visualized as sending message (outdata) to objects s2's class to show object contents. Therefore, by its very nature, Object Oriented computation resembles a client-server- computing model.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd