5. Introduction to classes.
Hello folks!
Welcome to the blog series. In the previous blog we have studied the
different types of variables that we are going to use in our object
oriented programming (OOP) project.
Now
in this blog we will discuss about Class and different topics
related to it.
A class in C++ is a user-defined type or
data structure declared with keyword class that has data and functions (also
called member variables and member functions) as its members whose access is
governed by the three access specifiers private, protected or public. By default
access to members of a C++ class is private. The private
members are not accessible outside the class; they can be accessed only through
methods of the class. The public members form an interface to the class and are
accessible outside the class. -Wikipedia.
Instances of
a class data type are known as objects and can contain member
variables, constants, member functions, and overloaded operators defined by the
programmer. -Wikipedia.
Classes are defined using
either keyword class or keyword struct, with the following syntax:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
The
syntax for defining a class is similar to that of defining a structure in C.
Here
the class_name is the identifier for the class (basically the
name of our class), object_names is the list of names
for objects of this class (optional). The body of the
declaration can contain members, which can either be data or function
declarations, and optionally access specifiers.
The access specifier can be one of the following three
keywords: private, public or protected.
(As discussed in earlier blog).
Any
member that is declared before any other access specifier or without any access
specifier has private access automatically.
Let us see an example of it.
The
output of above code is
Classes allows both Data and functions
as members of the object. Thus using principles of object oriented programming.
More topics related to class are:
1. Abstraction and Encapsulation.
2. Classes defined with structure and union
3. Constructors.
4. Overloading Constructors.
5. Destructors.
6. Const Objects
7. Friend functions.
8. Function overloading.
9. Function overriding.
10.Operator overloading.
Right now we won’t go in depth of these
topics, we will surely explain these concepts if further required. So in this
blog we learned how to initialize a class and little bit of its concepts and
Object oriented paradigms and in next blog we will see different types of
memory allocation
By-
Ashutosh Bardapurkar (k-05)
Hrishikesh Deshpande (k-16)
Archit Hiwrekar (k-23)
Chinmay Kapkar (k-33)
Impressive!!
ReplyDeleteThanks for your comment sir
DeleteNice work. Helped me a lot as a C++ beginner
ReplyDeleteThanks
Delete