Class and Objects

17

description

Class and Objects. Topics to be Discussed…. Class Classes in C++ Class Example Object of a Class Accessing Class Members Defining Member Functions Memory Allocation for Objects. Class. A Class is a user defined datatype which holds both data members and member functions. - PowerPoint PPT Presentation

Transcript of Class and Objects

Page 1: Class and Objects
Page 2: Class and Objects

Class Classes in C++ Class Example Object of a Class Accessing Class Members Defining Member Functions Memory Allocation for Objects

Page 3: Class and Objects

A Class is a user defined datatype which holds both data members and member functions.

Data members are data items related to Class and Member Functions are functions related to class which indicate different operations that can be performed on data members.

BACK

Page 4: Class and Objects

A class definition begins with the keyword class.

The body of the class is contained within a set of braces, { } ;

class class_name{

….….….};

Class body (data member + methodsmethods)

Any valid identifier

Page 5: Class and Objects

Within the body, the keywords private and public specify the access level of the members of the class.◦ the default is private.

Usually, the data members of a class are declared in the private section of the class and the member functions are in public section.

Page 6: Class and Objects

class class_name{ private:

………

public:………

};

Public members or methods

private members or methods

Page 7: Class and Objects

Member access specifiers◦ public:

can be accessed inside as well as outside the class.◦ private:

Accessible only to member functions of class Private members and methods are for internal use

only.

BACK

Page 8: Class and Objects

This class example shows how we can encapsulate (gather) a student information into one package (unit or class)

Class student { private: int roll; char name[25]; public: void getdata(); void display();};

No need for others classes to access and retrieve its value directly. Theclass methods are responsible forthat only.

They are accessible from outsidethe class, and they can access theData members

BACK

Page 9: Class and Objects

Declaring a variable of a class type creates an object. You can have many variables of the same type (class).◦ Instantiation

Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code

Syntax: classname listof objects; student x,y;

Page 10: Class and Objects

Objects can also be created when a class is defined by placing their names immediately after the closing brace.

Class student { private: int roll; char name[25]; public: void getdata(); void display();}x,y;

BACK

Page 11: Class and Objects

Any Public Member of the class can be accessed with the help of following syntax:

a) object name.membername; b) objectname.functionname(actual arguments);

Class Abc { private: int a; int b; public: int c; void sum();};Void main(){ Abc x;x.c=101; // accessing the data memberx.a=101; // error,access denied because a is a private memberx.sum(); // accessing the member function} BACK

Page 12: Class and Objects

Member functions can be defined in two ways:

1. Member functions defined outside class

Using scope resolution operator (::) “Ties” member name to class name Uniquely identify functions of particular class Different classes can have member functions with same

name

◦ Format for defining member functions

ReturnType ClassName::MemberFunctionName( ){

…}

Page 13: Class and Objects

Class Abc { private: int a; int b; public: int c; void sum();};Void Abc :: sum(){Cout<<“sum is =“<<a+b;}

Page 14: Class and Objects

2. Member functions defined inside the class

Can be defined within the class at the time of declaration Without Using scope resolution operator (::)

Class Abc { private: int a; int b; public: int c; void sum() { Cout<<“sum is =“<<a+b; } };

Page 15: Class and Objects

#include<iostream.h>

#include<conio.h>

Class abc

{

int a,b,c;

Public:

Void getdata(int,int);

Void putdata();

Void sum();

};

Void abc::getdata(int I,int j)

{

a=i;

b=j;

}

Void abc::putdata()

{

Cout<<“sum is =“<<c;

}

Void abc::sum()

{

C=a+b;

}};

Void main()

{

Abc x;

x.getdata(1,2);

x.sum();

x.putdata();

}

BACK

Page 16: Class and Objects

Memory is allocated to each of the object for data members.data members of each object are stored seperately but member functions are stored only once.

Space is allocated to member functions when class is declared.

Space is allocated for data members when objects are created.

BACK

Page 17: Class and Objects