Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Engineering Science Option Course [ESO3]

Course Code: CSE202 Lecture #05


Winter 2022-23

Classes and Objects in C++

Dr. Monidipa Das


Assistant Professor
Department of Computer Science and Engineering
Indian Institute of Technology (Indian School of Mines) Dhanbad, Jharkhand 826004, India
Structure and Class 2

• C structures do not permit data hiding


• C and C++ structure members are by default public members
• C++ structure can have functions as member
• C++ structure can explicitly make some of the members private
• C++ structure names are stand alone

• By default the members of a class are private

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad


Class Specification 3

• Class specification has two parts:


– Class declaration class item
{
– Class function definitions int number;
float cost;
class class_name
{ public:
private: void getdata(int a, float b);
variable declarations; void putdata(void);
};
function declarations;
public:
Data members
variable declarations;
function declarations;
}; Member functions

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad


Creating Objects 4

• Once a class has been declared, we can create variables of that type by
using class name (like any built-in type variable)
item x;
• In C++, class variables are known as objects class employee
• X is object of type item {
private:
• Objects can also be created when a class is declared. char name[20];
int age,sal;
class item public:
{ void putdata();
- - -; };
- - -;
- - -;
}x,y,z; Array of objects employee emp[5];
Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad
Defining Member Functions 5

• Can be defined in two places


– Outside the class definition
– Inside the class definition
return-type class-name :: function-name(argument declaration)
{
Function body;
}

void item :: getdata(int a, float b) void item :: putdata(void)


{ {
number=a; cout<< “Number: ”<< number <<“\n”;
cost=b; cout<< “Cost: ”<< cost <<“\n”;
} }

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad


Defining Member Functions [contd.] 6

• Inside the class definition


– Treated as inline function
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void)
{
cout<< “Number: ”<< number <<“\n”;
cout<< “Cost: ”<< cost <<“\n”;

}
};

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad


Defining Member Functions [contd.] 7

• Making outside function inline


class item
{
int number;
float cost;
public:
void getdata(int a, float b);
};

inline void item :: getdata(int a, float b)


{
number=a;
cost=b;
}

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad


Accessing Class Members 8

• Private data can be accessed only through the public member


functions of that class
object-name.function-name (actual-arguments);

• Example item x;
x.getdata(100,75.5);
x.putdata();

getdata(514,1000.0); //no meaning


x.cost=2500; //illegal since cost is private

• Public variable can be accessed by the object directly


Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad
9

Questions?

Prof. Monidipa Das, Department of CSE, IIT (ISM) Dhanbad

You might also like