object-oriented programming
The fundamental idea behind object-oriented languages is to combine into
a single unit both data and the functions that operate on that data. Such a
unit is called an object.
An object’s functions, called member functions in C++, typically provide
the only way to access its data.
You can’t access the data directly. The data is hidden, so it is safe from
accidental alteration. Data and its functions are said to be encapsulated into
a single entity. A key feature of object-oriented programming is data
hiding. It means that data is concealed within a class so that it cannot
be accessed mistakenly by functions outside the class. Data
encapsulation and data hiding are key terms in the description of object-
oriented languages.
Classes
In C++, the class forms the basis for object-oriented programming. A
class declaration defines a new type that links code and data. This
new type is then used to declare objects of that class. In other words,
an object is an instance of a class.
A class declaration is similar syntactically to a structure. A structure is used to
hold only data and a class is used to hold data and functions .
A class is away to bind the data and its associated functions together. When
defining a class, we are creating a new abstract data type that can be treated
like any other built-in data type.
A class has two parts
1. Class declaration
2. Class function definition
The general form of a class declaration
class name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
The members that have been declared as private can be accessed only from
within the class.
The members that have been declared as public can be accessed from outside
the class also.
The keyword private is optional. By default the members of a class are
private. If both lables(private, public ) are missing, all the members are
private. Such a class does not serve any purpose.
The variables declared inside the class are known as data members and the
Function are known as member functions.
A typical class declaration
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
Creating object
item x,y,z;
OR within class definition
calss item
{
int number;
float item;
public:
void getdata(int a, float b);
void putdata(void);
}x,y,z;
Defining member functions
1. Outside the class definition
2. inside the class definition
1. Outside the class definition
return_type class_name::function-name(argument declaration)
{
Body 2. inside the class definition
} class item
{
Ex)
int number;
void item::getdata(int a,float b)
float cost;
{ number=a;
public:
cost=b;
void getdata(int a, float b);
} void putdata(void)
void item::putdata(void) { cout<<“number:”<<number<<“\n”;
{ cout<<“number:”<<number<<“\n”; cout<<“cost:”<<cost<<“\n”;
cout<<“cost:”<<cost<<“\n”; }
} };
#include<iostream.h>
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void)
{ cout<<“number:”<<number<<“\n”;
cout<<“cost:”<<cost<<“\n”;
}
}; Output of the program
void item::getdata(int a,float b) number:100
{ number=a;
cost=b; cost:299.95
}
number: 200
main()
{ cost:175.5
item x;
x.getdata(100,299.95);
x.putdata( );
Item y;
y.getdata(200,175.5);
y.putdata( );
}