Classes and objects are fundamental concepts in object-oriented programming. A class defines the data and functions that characterize objects of a certain kind, acting as a blueprint. An object is an instance of a class, which reserves and allocates memory at runtime. Classes use access specifiers like public and private to determine whether data members and methods can be accessed within or outside the class. Objects are declared as instances of a class and can access class members using the dot operator. Member functions allow manipulating object data.
2. Class:
A Class is user define data type to implement an abstract object.
Abstract mean to hide the details.A class is a combinition of data
and function.
Data are called data member and function are called member
function.
. A Class is a blueprint for objects.
When a class is defined, no memory is allocated.
3.
4. A class definition begins with the keyword class.
The body of the class is contained within a set of braces{ } and semicolon with ending
braces.
{
…………..
..…………..
……………..
}:
Class class_name
{
some data
some
function
};
Class
declaring
Class
body
Class
end
5.
6. But remember class is never complete without access specifier.
The commands that are used to specify the access of class members are known as access specifier
There are three access specifier which are:
Private
Public
Protected
Above three access specifier are keyword which is written as it is but having different function.
7. Keyword private makes data and functions private and keyword public
makes data and functions public.
Private data and functions are accessible inside that class only whereas,
public data and functions are accessible both inside and outside the class. This
feature in OOP is known as data hiding. If programmer mistakenly tries to
access private data outside the class, compiler shows error which prevents the
misuse of data.
Generally, data are private and functions are public
9. Data members and member functions can be accesses by using member
operator(.).
The data member can be accessed as:
object_name.data_memeber;
And member function can be accessed as:
object.function name
10. Class sum
int a ,b , t;
Public:
Void get_data()
{
cout<<“enter value of a and b”;
cin>>a>>b;
}
Void put_data()
{
t=a+b;
cout<<“sum of a and b is:”<<t;
}
};
Int main()
{
sum obj;
Obj.get_data();
Obj.put_data();
getche();
return 0:
}
11. If we want to define member function outside the class
we can used it with the help of scope resolution
operator(::).
12. Class student void student::show_data()
{ {
Private: cout<<“name of student is
int roll; :”<<name;
Char name[10]; cout<<“roll no of student
Public: is:”<<roll no;
Void get_data(); }
Void show_data(); int main()
}; {
void student::get_data() ……………
{ ……………….
cout<<“enter name”; getche();
cin>>name; return 0;
cout<<“enter roll no”; }
cin>>roll no;
}
13. We can also declare a class inside a function when class declared inside
a function is called local class
A local is only known to that function and unknown inside it.
Due to these restriction local class is not so popular in c++.
14. Object is an abstraction of real world entity.
An object is an instance of a class.
An object is a class variable.
It can be uniquely identified by its name.
15. Syntax for declaring object is as fallows:
Object_name.member function(parameter list);
16. Class sum
int a ,b , t;
Public:
Void get_data()
{
cout<<“enter value of a and b”;
cin>>a>>b;
}
Void put_data()
{
t=a+b;
Cout<<“sum of a and b is:”<<t;
}
};
int main()
{
sum obj;
obj.get_data();
obj.put_data();
getche();
return 0:
}
17.
18. This can be done by two ways.
A copy of entire object is passed to the function(i.e. pass by value)
Only the address of the object is transferred to the function(i.e. pass by
reference).
19. A program to demonstrate passing objects by value to a member function of the same
class
#Include<iostream.h>
class weight
{
int kilogram;
int gram;
public:
void getdata ();
void putdata ();
void sum_weight (weight,weight) ;
} ;
void weight :: getdata()
{
cout<<"/nKilograms:";
cin>>kilogram;
cout<<"Grams:";
cin>>gram;
21. The output of the program is
Enter weight in kilograms and grams
Enter weight #1
Kilograms: 12
Grams: 560
Enter weight #2
Kilograms: 24
Grams: 850
Weight #1 = 12 Kgs. and 560 gms.
Weight #2 = 24 Kgs. and 850 gms.
Total Weight = 37 Kgs. and 410 gms.