Classes in C++ allow developers to combine data and functions that operate on that data into logical units called objects. A class defines the form and behavior of objects, while objects are individual instances of a class. Some key points:
- Classes contain data fields called data members and functions called member functions.
- Objects are instantiated from classes and allocate memory to store values for an instance.
- Access specifiers like public, private, and protected determine which parts of a class are accessible from outside the class.
- Classes can contain static members that are shared across all objects of the class rather than being unique to each object.
2. Class
A class is an organization of data and
functions which operate on them.
Data structures are called data members and
the functions are called member functions.
The combination of data members and
member functions constitute a data object or
simply an object.
3. Object
Instantiation of a class.
In terms of variables, class would be the type
and an object would be a variable.
4. General Structure of a class
Class name or name of class
Data Members
Member functions
Access Specifiers
Declaring objects
5. Classes in C++
A class definition begins with the keyword
class.
The body of the class is contained within a set
of braces, { } ; (notice the semi-colon).
class class_name
{
….
….
….
};
Class body (data member
+ methods)
Any valid
identifier
7. Class name
Name given to a particular class.
Serves as a name specifier for the class using
which we can create objects.
The class is specified by keyword “class”
8. Data Members
Data type properties that describe the
characteristics of a class.
We can declare any number of data members
of any type in a class.
We can say that variables in C and data
members in C++.
E.g. int rn;
9. Member functions
Various operations that can be performed to
data members of that class.
We can declare any number of member
functions of any type in a class.
E.g. void read();
10. Outside class definition
Return type classname :: funcname(arg
declaration)
{
Function body
}
Void item ::getdata(int a,float b0
{
N=a;
C=b;
}
11. Inside class
Class item
{
int number;
Float cost;
Public:
Void getdat(int a,float b);
Void putdata(void);
{
cout<<number;
cout<<cost;
}
};
12. Making an outside function
inline
Objective of oop is to separate details of implementation from
class definition
class item
{
public:
void getdata(int a,float b);
};
inline void item :: getdata(int a,float b)
{
n=a;
c=b;
}
13. Access Specifiers
Used to specify access rights for the data
members and member functions of the class.
Depending upon the access level of a class
member, access to it is allowed or denied.
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.
14. Classes in C++
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or
methods
15. Private:
only members of that class have accessibility
can be accessed only through member
functions of that class.
Private members and methods are for
internal use only.
16. Public:
Accessible from outside the class
can be accessed through member function of
any class in the same program.
17. Protected:
Stage between private and public access.
They cannot be accessed from outside the
class, but can be accessed from the derived
class.(inheritance)
18. Class Example
This class example shows how we can
encapsulate (gather) a circle information into
one package (unit or class)
class Circle
{
private:
double radius;
public:
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
No need for others classes to
access and retrieve its value
directly. The
class methods are responsible for
that only.
They are accessible from outside
the class, and they can access the
member (radius)
19. C++ supports three access specifiers:
public
private
protected
The public access specifier allows a class to subject its
member variables and member functions to other functions and
objects
The private access specifier allows a class to hide its
member variables and member functions from other class
objects and functions
The protected access specifier allows a class to hide
its member variables and member functions from other class
objects and functions just like private access specifier - is
used while implementing inheritance
20. Declaring objects:
A class declaration only uses to build the
structure of an object.
Declaration of an object is same as declaration
of class.
Defining objects of class data type is known as
class instantiation(instances of class)
When we create objects during that moment ,
memory is allocated to them.
• Ex- Circle c;
22. Private member function
a private member function can only be called by
another function that is a member of its class. Even an
object cannot invoke a private function using dot
operator
class sam
{ int m; void read(void);
public:
void update(void); void write(void);
};
if s1 is an object of sam then
s1.read(); won’t work
Can be done like
void sam:: update(void)
{ read();
23. Arrays within class
const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
24. Memory allocation for objects
We have studied that memory space for
objects is allocated when they are declared
and not when class is specified. All objects
belong to a class use same member functions
no separate space is allocated for member
function when object is created.
Only space for member variables is allocated
separately for each object.
Memory created when functions defined:
common
Memory created when objects defined. Not
common
26. Static data members
It is initialized to zero when first object of its
class is created . No other initialization is
permitted.
Only one copy of that member is created for
entire class and shared by all objects of that
class.
Visible only within class but lifetime in entire
program.
Static int age;
27. Static class member
class item
{ static int count;
int number;
public:
void getdata(int a)
{ number = a;
count++;
}
void getcount(void)
{
cout<<count;
}
};
Int item :: count; // definition of static data member
main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
Cout<<“ after reading”;
a.getcount();
b.getcount();
c.getcount();
}
OUTPUT
Count:0 0 0
After reading 3 3 3
28. Static member function
A static function can have access to only other
static members declared in same class
Can be called using class name instead of
objects
Class-name :: function-name;
29. Class test
{
Int code;
Static int count;
Public:
Void setcode(void)
{
Code= ++count;
}
void showcode(void)
{ cout<<code;
}
Static void showcount(void)
{
cout<<count;
} };
int test :: count;
int main()
{ test t1,t2;
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode();
test:: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
31. Object as function argument
Entire object is passed as function argument.
Pass by value: no changes made to the object.
Only address is passed to function.
Pass by reference: address is passed.
33. What is this pointer?
Every object has a special pointer "this" which
points to the object itself.
This pointer is accessible to all members of
the class but not to any static members of the
class.
Can be used to find the address of the object
in which the function is a member.
Presence of this pointer is not included in the
sizeof calculations.
35. // Not using this pointer
void MyClass::Print1() {
cout << data << endl;
}
// Using this pointer
void MyClass::Print2() {
cout << "My address = " << this << endl;
cout << this->data << endl;
}
int main()
{
MyClass a;
a.Print1();
a.Print2();
// Size of doesn't include this pointer
cout << sizeof(a) << endl;
}