Object Oriented Programming Features Part 3 | C ++ Tutorial | Mr. Kishore
Summary
TLDRIn this 'Nourish 80' episode, Kishore dives into object-oriented programming, focusing on the distinctions between C structures and C++ classes. He elucidates that C++ classes support both variables and functions, unlike C structures, and highlights the default privacy of class members versus the public nature of C structure members. Kishore also explains concepts like encapsulation, data hiding, and inheritance, which are pivotal to object-oriented programming. The video is a comprehensive guide for beginners looking to understand the fundamentals of classes in C++.
Takeaways
- 📘 The class in C++ is an extension of the C structure, allowing both variables and member functions within its definition.
- 🔑 A major difference between C structures and C++ classes is that by default, C structure members are public, while C++ class members are private.
- 🏭 C++ classes support the concept of inheritance, unlike C structures, which cannot be used as a base for deriving other structures.
- 👥 C++ classes are used for constructing complex programs due to their support for inheritance, while structures are typically used for simpler programs.
- 📦 Classes in C++ act as a blueprint for creating objects, serving as a logical copy from which multiple physical instances (objects) are made.
- 🔒 Classes provide the concept of encapsulation, bundling data and member functions into a single unit and controlling access to the data through member functions.
- 🙈 Data hiding is achieved in classes through private access specifiers, ensuring that class data is not directly accessible from outside the class, promoting security.
- 📝 Member functions can be defined both inside and outside of the class definition, with different implications for inline functions and scope resolution.
- 👀 Access to private class members must be done through public member functions, illustrating the principle of data hiding and encapsulation.
- 🛠️ The class syntax includes the class keyword, class name, access specifiers, data members, member functions, and ends with a semicolon.
- 📚 Understanding the differences between C structures and C++ classes is crucial for utilizing object-oriented programming features effectively in C++.
Q & A
What is the main difference between C structures and C++ classes?
-The main difference is that C structures only allow variables inside them, while C++ classes allow both variables and member functions, which are functions defined within the class.
What are the three types of access specifiers provided by C++ classes?
-The three types of access specifiers are private, public, and protected. These specifiers determine the visibility and accessibility of the class members.
Why are member functions in a class necessary when the data members are private?
-Member functions are necessary to access and manipulate private data members, as direct access from outside the class is restricted. This is part of the encapsulation concept in object-oriented programming.
What is the purpose of encapsulation in object-oriented programming?
-Encapsulation is the mechanism of binding data (variables) and member functions together into a single unit, which is the class. It is used to hide the internal state of an object and require all interaction to be performed through an object's methods.
What is data hiding in the context of classes?
-Data hiding is the concept where the internal data (data members) of a class are kept private and are not directly accessible from outside the class, ensuring that the data can only be manipulated through member functions.
Why is it important to have at least one public member function in a class?
-At least one public member function is important because it allows for interaction with the class's private data members. Without a public interface, the class would be inaccessible and thus, not useful.
What is the default access level for class members in C++?
-By default, class members in C++ are private, meaning they are not accessible from outside the class unless specifically made public or protected through access specifiers.
Can structures in C++ be used for inheritance?
-No, structures in C++ do not participate in inheritance. Only classes can be used as base classes to derive other classes.
What is the difference between defining a member function inside and outside the class?
-Defining a member function inside the class makes it an inline function, which can be expanded at the point of use by the compiler. Defining it outside the class requires the use of the class name and scope operator, and it does not have the inline property.
How is an object of a class declared in C++?
-An object of a class is declared by specifying the class name followed by the object name. For example, if the class name is 'Student', an object can be declared as 'Student s;'.
What is the role of the scope operator (::) when defining a member function outside the class?
-The scope operator (::) is used to specify that the function being defined is a member of the class. It distinguishes the member function from global functions with the same name.
Outlines
📚 Introduction to Object-Oriented Programming
The script begins with an introduction to Object-Oriented Programming (OOP) by Kishore, focusing on the continuation of OOP concepts from a previous session. It explains the basic difference between C structures and C++ classes, highlighting that C++ allows both variables and functions within a class, unlike C structures which only allow variables. The explanation includes the default access levels of structure members in C and C++, emphasizing that C++ class members are private by default, unlike C structures which are public. The paragraph sets the stage for a deeper dive into OOP features.
🏗️ Differences Between C Structures and C++ Classes
This paragraph delves into the distinctions between C structures and C++ classes. It clarifies that while both are user-defined and complex data types, C++ classes differ by being involved in inheritance, allowing for the derivation of one class from another, which is not possible with structures. The paragraph also touches on the use case scenarios, suggesting that structures are typically used for simpler programs, whereas classes, due to their inheritance capabilities, are better suited for building complex applications. The summary of similarities and differences is aimed at helping understand the appropriate use of each.
🔏 Class Concepts: Encapsulation and Data Hiding
The third paragraph discusses the concept of encapsulation in classes, which is the bundling of data and functions into a single unit. It also explains data hiding, a feature that keeps the class's data private, accessible only through member functions, thus securing the data from external access. The paragraph emphasizes that classes provide a blueprint for creating objects, serving as a logical plan that does not occupy memory, unlike objects which are physical instances and do require memory allocation.
📝 Class Syntax and Access Specifiers
This section provides an overview of the syntax for declaring a class in C++. It explains the use of access specifiers such as private, public, and protected, which determine how class members are accessed. The paragraph clarifies that by default, class members are private, meaning they are not accessible outside the class unless through a public or protected interface. It also touches on the syntax for declaring objects and the importance of ending class declarations with a semicolon.
🚫 Private Members and Data Access in Classes
The focus of this paragraph is on the private nature of class members and how they are accessed within the class. It explains that private data members cannot be directly accessed or initialized outside the class or by objects, but must be manipulated through member functions. The paragraph also discusses the error that occurs when attempting to access private members directly and the necessity of having at least one public member function to make the class useful.
🔄 Member Function Definitions Inside and Outside the Class
This paragraph explains the two ways of defining member functions in C++ classes: inside the class declaration, which makes the function an inline function, and outside the class declaration, which requires the use of the class name and scope operator. It provides an example of a class with private data members and a public member function for accessing and modifying the data. The summary highlights the importance of proper member function definition placement and the mechanics of memory allocation when objects are created.
🔚 Conclusion and Future Topics
The final paragraph wraps up the current discussion on classes and member functions, indicating that future sessions will continue with the remaining topics in object-oriented programming. It provides a brief mention of the next topics, such as friend functions and pointers, setting the stage for further learning and exploration in the realm of C++ and OOP.
Mindmap
Keywords
💡Object-Oriented Programming (OOP)
💡Class
💡Object
💡Encapsulation
💡Data Members
💡Member Functions
💡Access Specifiers
💡Private Members
💡Public Members
💡Inheritance
💡Data Hiding
Highlights
Introduction to Object-Oriented Programming (OOP) features in C++
Difference between C structures and C++ classes, including access to member functions
C++ classes allow both variables and functions unlike C structures
C structures are public by default, while C++ class members can be private, public, or protected
Explanation of the two major differences between C structures and C++ structures
Introduction to the concept of classes in C++ and their differences from structures
Classes in C++ are user-defined and complex data types
Classes are collections of variables (data members) and functions (member functions)
Classes provide encapsulation, binding data and functions into a single unit
Classes offer data hiding through private member declarations
Classes serve as blueprints for creating objects, occupying no memory unlike objects
Syntax of a class in C++, including access specifiers and member declarations
Default access levels for class members in C++, with private being the default
Inheritance is a key feature of C++ classes, distinguishing them from structures
Classes are suitable for building complex programs due to inheritance capabilities
Member function definitions can be inside or outside the class in C++
Accessing private class members through public member functions
Declaration of class objects and memory allocation for each object
Conclusion and continuation of the remaining OOP topics in future sessions
Transcripts
[Music]
hi welcome to nourish 80 this is Kishore
and today we are going to continue the
object-oriented programming features in
last session we have discussed some of
the object-oriented features such as
class and object today we are going to
continue the remaining features of
object-oriented programming now first of
all I am giving brief examples of class
and object first what is a class what is
an object okay now say this class is the
extension of C structure and the major
difference between C structure and C++
classes C structure allows C structure
arrows only the variables inside the
structure which are called structure
members but C++ class hellos both the
variables under member functions means
nothing but functions which are called
member functions that is why here I am
giving the differences between C
structure C++ structure and this class
understructure simplest way structure
first I am going to give structured C
structure differences and C++ structure
okay that is why first it is the C
structure and it is the C++ structured
now first thing is C structure is
collection of variables and C++
structure is collection of variables and
functions next C structure is by default
public that means structure members are
by default public X here C++ structure
members are private or public are
protected okay we can declare the C++
structure members in three ways one is
private mode another one is public mode
and another one is protected number now
these
are the two major differences in between
the C structure and C++ structure and
working style everything is similar to C
that means both working style and
everything is same to same the only
differences first one is collection of
variables means a see structures doesn't
allows functions inside the structure
and C++ allows both to the variables and
functions inside the structure and here
see structure members are by default
public that means anybody can that means
anybody can means here in the total
program we can access the structure
members from anywhere but here they are
private public protected these are the
two major differences between the C
structure and C++ structure now I said
C++ classes okay we are declaring the
class are we are getting the class from
C structure that is why now I am giving
what is the difference between structure
and class okay these are these are the
differences between C structure and C++
structure now I am going to view the
differences between structure and class
okay now first one C++ structure and it
is the class now the first difference is
of course generally everybody knows it
is a user-defined and here also it is
user-defined
okay now I am giving similarities and
differences both that is why here C++
structure is called user defined class
also called
user-defined okay next it is a complex
data type here also same thing complex
data type next here
the point is collection of variables and
functions
simplify structure is the collection of
variables and functions as usual class
also collection of variables and
functions next now fourth one members or
private or public are predicted
now simple structure members are private
public or product and the same thing is
happening here numbers are private are
public are protected now these are the
common things they are looking to be say
okay now the four properties are looking
to be same now the difference is what ok
here the first major difference is here
by default members are public by default
members or public that means when there
is no visibility label or access
specifier is not mentioned ok when no
visibility label or x4 is not mentioned
automatically all the members will
become public members that means anybody
can access from anywhere and here by
default class members are private it is
a major difference by default class
members are private that means only the
authorized it means only the member
functions of same class are accessible
outside members are not accessible it is
the first major difference in between
the C++ structure and class next here
not inherited it is the major another
major difference okay structures are not
participated in inheritance means from
one structure we are not able to derive
another structure but classes are
inherited okay from one class we can
declare another class or from one class
we can derive another class that is why
classes are participating in inheritance
next you would to build simple data
types our programs generally structures
we are using for constructing a simple
programs that means not the chrome
complex programs okay for example bank
examples railway reservations like that
okay in big projects we are not able to
use the structure concept only they are
limited for simple programs under here
they are easier for complex programs
easy to build complex programs that is
why by using the class concept because
of inheritance is alone okay
due to that future using C++ classes we
can construct a big projects it is the
major advantage of class these are the
similarities and differences between the
C++ structure and the class okay now it
is nothing but a similarities and
differences of class next now how to
declare the class and what are the
definition of class okay now I am giving
class a string it is a user-defined data
type okay class is called it is a
user-defined data type because of we are
constructing the class by using the
primitive and a derived data types as
per the user requirement that is why
class is called it is a user-defined
datatype next it is a complex data type
class is a complex because of different
types of data types are available at one
place that is why it is a complex data
type next it is a it is collection of
members it is collection of members that
means class
contains different types of numbers what
they are means one data members nothing
but variables mixed second one is number
functions
nothing but functions okay let us say
here the point is classic members that
means what class contains both the data
members and member functions which are
collectively known as members that means
here data members means nothing but a
variables that are declared inside the
class are called data members and the
functions that are declared inside a
class are called member functions and
collectively both are called members
okay
next class is a container that means
which contains the members because of
one container container means what which
contains something here it contains
something what data members and member
functions that is why class is a
container next class provides the
concept of encapsulation means what when
class is user to class contains what
both the data members and member
functions that means class allows to
bind class allows to hide different
types of variables and functions into a
single unit called class okay now class
allows to place different types of
variables and member functions are
functions into a single unit called
class and this mechanism is called
encapsulation okay it is nothing but
encapsulation next class provides the
concept of data hiding with private
declaration already we have discussed
class members are either private or
public are protected okay here the main
point is when the members are private
only the member functions of that class
are able to access the data that means
outers are not allowed at any cost
except the pointers friend functions
except the pointers and friend functions
which are discussed in later sessions
okay here that is why an important point
of classes when the class data is
private it is not visible outside the
class that is called data hiding that is
why the key factor of data hiding is
achieved with the private declaration
that LS well allowed by the class the
swea class provides the concept of data
hiding with the private declaration next
class easy blueprint means original copy
to construct the objects that's why here
on important thing is class is a
blueprint class is a blueprint here
blueprint means original copy to
construct objects that means to
construct the objects first of all we
should have to define the class because
of class is the nothing but a plan okay
that is that is it is a plan get here
watch it it is a plan before
constructing the objects they say to
construct the objects first of all we
have to make a plan for example to
construct a building we should have to
go for ok plan that means we are going
to meet the planner which is also called
architect ok first of all we are going
for civil engineer who is also called
architect now he is going to draw a plan
on the paper
based on that plan the Builder is going
to construct the building that is why
here one most important point is
class is a plan class is a plan means
paper plan which is used to construct
the objects in my example paper plan is
the class and the real building is the
objects that is why class is a logical
copy to construct the physical copies
which are called objects that is why
class is a okay here it is the most
important point class is a logical copy
to create objects because of here class
never occupies any memory okay here
because of class never occupies any
memory and objects always occupies the
memory that is why objects are called
physical representation of classes now
these are the important features of
class that means what is a class under
what class provides that is why class
means it is a user-defined data type
okay here class is a user-defined data
type X it is a complex data type it is
collection of members what are the
numbers available means data members
member functions and what is called
later member means the variables that
are declared inside the class are called
data members and the functions are
called member functions that is why
class is nothing but a container which
contains the both data members and
member functions and class provides the
concept of encapsulation and capsulation
refers to be-what binding of data and
member functions together into a single
unit called
class next class provides the concept of
data hiding that means what outers are
not tell or means the private data is
fully secured and it should be accessed
with only the member functions of that
class it is not visible outside the
class it is called data hiding feature
and here class is a blueprint just
before you have finished
to construct the objects we have to draw
the plan and that plan is nothing but
class and the plan is called blueprint
blueprint means to original copy okay
next it is a plan before constructing
the objects that is why class is a
logical way because class never takes
any memory only the object takes the
memory that is why class is a logical
copy and objects are the physical
instances of the class okay next what is
the syntax of a class okay now I am
going to give syntax of the class
generally the class is started like this
class and here class name and class name
is optional when the object is declared
immediately after the class here class
is a keyword the denotes we are going to
start a class here and here we have to
enter the class name and class name is
optional when the objects are declared
immediately next here visibility label
our axis specifiers next data members
nothing but variables and member
functions member functions nothing but
functions next it is the class closing
and here one important thing is here we
can declare the class variables which
are called objects and here important
thing is when the class variables or
objects are declared okay and the class
variables or objects are declared
immediately after the class means here
then class name is not required it is
purely optional but whenever the class
variables are not declared here we have
to declare some other places of the
program then the class name is mandatory
and every class should have to end with
the semicolon it is another important
thing
now it is nothing but the syntax of a
class thus every class should have to
start with class keyword and every class
is having a particular name it is
optional when the variables are declared
immediately next access specifier now
what is called access specifier the name
is very clear it specifies how they are
going to be accessed okay
it specifies how they are going to be
accessed in our programs under class
provides three types of access
specifiers one is private another one is
public and another one protected okay
insular C++ we are going to use three
types of access specifiers or visibility
labels now we are going to use private
public protected now what is happening
when private and public unprotected I am
going to explain what is happening when
private is is serious suppose I am going
to use private declaration now whenever
the data members or number functions are
private okay they are directly not
accessible with the objects for example
I am going to declare int a now see this
is called data number and here I have
not used it any access specifier means
either private or public or protector
now automaticall pilar assumes it is a
private because of by default class
members are private okay remember this
by default all the class members are
private when class members are private
okay especially data members are private
they should be accessible with the
member function for example now I am
going to write here why you get just you
zoom get is one of the member function
and here inside this a equal to hundred
later c OT no function flows and it is
100% allowed
because of here the rule is private data
should be accessed with member functions
of same class and here a is declared
inside of the class get also declared
inside the class that is why they are
called numbers that is why private deter
should be accessed with another member
functional ok the so it is except there
is no problem and one more most
important point in say + place it is not
allowed int equal to for example hundred
now this kind of declaration use error
number function not declared here member
function not initialized here error it
is going to show that is why members
data member should be initialized or
user or access it with a member function
only director initializations are not
allowed it is one of the most important
role next
now what happens watch it it is provided
and it is also private number function
because of no access specifier mentioned
now a Terry I want to access this member
function suppose just as you it is the
main function here I have declared one
object for example it is the object now
object two dot a equal 100 now it gives
error an objective dot get it also gives
error okay both in both situations we
are getting error because of now a is a
private member get also private number
where members are private they are
directly not accessible with the objects
okay it is a most important role in
class okay when the data members are
member functions are private they should
okay they are not available to the
object directly using dot membership
operator here I have user dot what is
called membership operator dot is called
what number ship operator and when the
members are private we are not able to
access with the object name directly
that is why you
is better to use one public member
function now public now what happened a
will become private member but gate will
become public member and public number
directly accessible with the object that
is why it is not allowed but this one is
allowed object to not gate okay that is
why to access the members okay
that is why what I want to say is when
the all when all the class members are
private it is useless okay
why because to access the up class
numbers we should have to declare the
object and where the object is generally
declared and where the members are
called from main function actually main
function is outside the class but in CPP
the main rule is what members are not
visible private members are not visible
outside the class but what happened now
the private member is a private member
get also private number that is why it
is not visible which is called data
hiding that is why when they are not
visible how can we access that is why
this class is useless class that is why
just to declare at least one class as a
public number otherwise use the friend
functions otherwise use the points okay
but later we are going to discuss
foreign function because of right now we
are for sure that is why I am NOT going
to discuss about fine function and
pointers and now thus why you have to
maintain at least one public number
function inside the public member
function we can call the private members
that is why now I am going to give
picture full picture clarity watch this
I am going to give the public private or
protected because right now I am NOT
able to give the protected members
because of the protected members are
used only in inheritance concept when
inheritance is started there I am going
to explain what is protected member now
I am going to give private and public
declarations and how the private members
are accessed inside the class now just
example see this class for example
student
now private and here every access
specifier should have to end with :
symbol it is the most important role
next int ID next character name next
float fee now these three members are
called data members next I am going to
start like this for example I want to
make public then directly public and it
is another axis specifier now why did I
get here I am going to write like this
see out enter ID comma name comma feed
now CN is required okay
see in ID name see here Co t is what see
n is odd because of we have not
discussed enough that's why I am giving
small karate see this here Co T is
nothing but printf in C language and
scene is nothing but scanf in c language
that is why one is for output purpose
one is for input it is going to printer
the matter it is going to read the
values okay now it is the program later
I want to put into the data void see out
ID equal to ID and and L means nothing
but a /nc language generally in c
language we are using / installation
means what next line the so here and L
is the manipulator which is used for
next line next co t name no name is
printed and l and c out fee fee also
print no function closed class closed it
is the example and here private and
public they are called access specifiers
okay here private and public are called
taxes specific ID name fee they are
nothing but data members and gate is the
member function that is why class is
collection of data members and member
functions with the access specifiers and
now watch it what happens it is the
class declaration and definitions and
here in C++ we can define the member
functions in two ways one is inside the
class by using this method now your
compiler understands it is a inline
function what is a inline function in
later session we are going to start okay
after that we are going to discuss that
because of it is a different concept
inline function is what and now here the
definition is completed within the class
that is why it is called okay member
function definition inside the class and
we can define the member function
outside of the class also now I am going
to give how outside of the class it is
defined simple logic just in case of
this one see this suppose public wide
get no class close okay now the class is
closed
now here the role is whenever the
definition is outside we should have to
start the class name s T u : : gate and
here it is not a inline function
remember this when the definition is
completed inside the class it will
become inline function but it is Right
notes but whenever the definition is
completed outside of the class you
should have to use classname and scope
operator it is a most important role
when the definition is completed outside
of the class you should have to start
with the class name first let us cope
operator later member function now in
two ways we can define the member
function one is inside one is outside it
is the example for outside another one
inside next now I want to access this
one I want to access the function and
gate is the public member or private
member except it is exactly public
member that is why here I am going to
declare the object void
mein now how to declare the object
class-name first space object that's all
now it is the syntax to declare the
object that is why first class name
object here our class name is what
student that is why in class name st you
later object name is user-defined
suppose yes now this is the object okay
when object is created what happens the
memory is allocated here ID is their ID
which type integer
the SOI two bytes name here name we have
to store several characters now 20
characters float fee four bits because
of float the so four plus twenty plus
two total 26 bytes memory allocated in
stack it is the ID it is the name and it
is the fee it takes two bites only 20
bytes it for total 26 bytes and gate
function is there now Kanaka I am going
to call like this s dot get it is called
what calling plus at our program plus
when this function is called get is
going to call this one and it is going
to read and print the data it is how to
access the class members it is how to
access the class members okay in later
session we are going to continue the
remaining topic okay thank you for
watching
you
[Music]
Weitere verwandte Videos ansehen
Features of Object Oriented Programming Part 2 | C ++ Tutorial | Mr. Kishore
Object Oriented Programming Features Part 4 | C ++ Tutorial | Mr. Kishore
Introduction to C++ | Lecture 1 | C++ and DSA Foundation Course
COS 333: Chapter 12, Part 2
COS 333: Chapter 12, Part 1
OOPS Interview Questions and Answers | Object Oriented Programming Interview Questions C#
5.0 / 5 (0 votes)