Object Oriented Programming Features Part 4 | C ++ Tutorial | Mr. Kishore

Naresh i Technologies
7 Jan 201714:55

Summary

TLDRIn this Nourish 80 video, Kishore explores the concept of objects in C++, explaining how they are created and how memory allocation occurs. He clarifies that classes are logical representations, while objects are their physical counterparts, requiring memory. Kishore also discusses the two ways to define member functions: inside or outside the class, emphasizing the importance of not including complex expressions within inline functions. The video serves as an educational guide on object-oriented programming, highlighting encapsulation as a key feature that binds data members and functions into a cohesive unit.

Takeaways

  • 📘 The concept of a class in C++ is an extension of the C structure, allowing for the storage of various types of variables and member functions.
  • 🔑 To access class members, an object of the class must be declared because the class itself is a logical representation and does not allocate memory until objects are defined.
  • 🏷️ By default, all class members are private if no access specifier is mentioned, adhering to the principle of encapsulation.
  • 🔄 Member functions can be defined inside or outside the class, with definitions inside the class being inline functions and not suitable for complex expressions or loops.
  • 📝 When defining member functions outside the class, the scope operator (::) is used to indicate that the function belongs to the class.
  • 🔑 To call member functions or access class members, at least one object of the class must be declared, as objects are the physical representation of the class.
  • 📚 An object is defined as a class variable, an instance of a class, and the physical representation of a class, requiring memory allocation.
  • 🔍 Each object created from a class can store data separately, allowing for the management of distinct data instances.
  • 🔑 Encapsulation is a key feature of OOP, binding data members and member functions into a single unit called a class, promoting data integrity and abstraction.
  • 📈 The script provides a foundational understanding of classes and objects in C++, emphasizing the importance of memory allocation and access methods for class members.
  • 🎓 The explanation of class and object definitions, along with the concept of encapsulation, sets the stage for further exploration of OOP features in subsequent sessions.

Q & A

  • What is the primary function of a class in C++?

    -A class in C++ serves as an extension of the C structure, allowing for the storage of different types of variables and member functions within it.

  • Why is it necessary to declare an object from a class?

    -Declaring an object from a class is necessary to access the class members because memory is not allocated when the class is declared; it is only allocated when objects are defined.

  • What is the default access specifier for class members if none is specified?

    -If no access specifier is mentioned, by default, all class members become private.

  • What are the two ways to define member functions in a class?

    -Member functions can be defined either inside the class, which makes them inline functions, or outside the class, which does not make them inline.

  • Why should looping statements or complex expressions be avoided when defining member functions inside the class?

    -Looping statements or complex expressions should be avoided in inline member functions because they can lead to code bloat and are not suitable for inlining.

  • What is the purpose of the scope operator (::) when defining a member function outside the class?

    -The scope operator (::) is used to indicate that the function defined outside the class belongs to the class, clarifying the association between the function and the class.

  • How does the creation of an object relate to memory allocation in C++?

    -When an object is created from a class, memory is allocated for that object, making it the physical representation of the class, whereas the class itself is a logical representation.

  • What is the difference between a class and an object in terms of memory allocation?

    -A class, when declared, does not allocate memory. Memory is allocated only when objects (instances of the class) are created.

  • How many objects can be declared from a single class?

    -You can declare any number of objects from a single class, each object having its own separate memory allocation for the class members.

  • What is encapsulation in the context of object-oriented programming?

    -Encapsulation is the process of binding data members and member functions into a single unit called a class, thus encapsulating the data and the methods that operate on the data within the class.

  • Why is encapsulation an important feature in object-oriented programming?

    -Encapsulation is important because it promotes the bundling of data and methods that operate on the data, providing data hiding and abstraction, which simplifies the management and maintenance of code.

Outlines

00:00

📘 Introduction to C++ Classes and Objects

This paragraph introduces the concept of classes and objects in C++. It explains that a class is an extension of a C structure, allowing the storage of different types of variables and member functions. The speaker clarifies the difference between declaring a class and defining an object, emphasizing that memory allocation occurs only when objects are defined. The explanation includes the default privacy of class members and the two methods of defining member functions: inline within the class and outside the class. The paragraph also touches on the importance of not including complex expressions or loops within inline functions.

05:00

🔍 Accessing Class Members and Member Functions

This section delves into how to access class members and member functions. It details the syntax for defining member functions both inside and outside the class, using the scope operator (::) to associate member functions with the class. The explanation includes how to declare objects to access class members and the distinction between logical and physical representations of data. The paragraph illustrates the process with an example of a 'student' class, showing how memory is allocated for objects and how to call member functions using the dot notation.

10:03

🏭 Understanding Objects and Encapsulation in OOP

The final paragraph provides a comprehensive understanding of what an object is in the context of object-oriented programming (OOP). It defines an object as a class variable, an instance of a class, and the physical representation of a class. The explanation covers the creation of multiple objects from a single class, each with its own memory allocation and data storage. The paragraph also introduces the concept of encapsulation, which is the bundling of data members and member functions into a single unit, and highlights its importance in OOP for managing data efficiently.

Mindmap

Keywords

💡Object

An 'Object' in the context of the video refers to a class variable or an instance of a class in C++. It is a physical representation of a class, meaning when a class is declared, memory is not allocated until an object is defined. For example, the script discusses creating objects 's1', 's2', and 's3' from the 'student' class, each with its own separate data storage for ID and name.

💡Class

A 'Class' is a blueprint or logical representation in C++ that defines the data members and member functions. It extends the concept of a C structure by allowing the storage of different types of variables and functions. The script explains that when a class is declared, it does not allocate memory; it's the creation of objects from the class that does.

💡Member Functions

Member functions are functions defined within a class in C++. They operate on the data members of the class. The video script mentions defining member functions both inside and outside of the class, with the distinction that defining them inside makes them inline functions, which should not contain complex expressions or loops.

💡Data Members

Data members are variables declared within a class that hold the state of an object. In the script, 'int ID' and 'char name' are examples of data members in the 'student' class. They are the properties that each object of the class will have.

💡Encapsulation

Encapsulation is an object-oriented programming concept that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit or class. The script describes encapsulation as the process that allows the binding of data members and member functions into a class, providing a way to manage data in a structured manner.

💡Scope Operator

The 'Scope Operator' (::) is used in C++ to define or access class members outside of the class definition. In the script, it is used to indicate that a member function defined outside the class belongs to the class, as in 'student::put'.

💡Inline Function

An 'Inline Function' is a function that is expanded in line at each point it is called, rather than being called through normal function call mechanisms. The script explains that defining a member function inside a class makes it an inline function, which should be simple and not contain loops or complex expressions.

💡Memory Allocation

Memory allocation in the context of the video refers to the process of reserving memory for variables or objects. The script clarifies that when a class is declared, no memory is allocated, but when objects are defined, memory is allocated for each object's data members.

💡Access Specifiers

Access specifiers in C++ are keywords (public, private, protected) that define the accessibility of class members. The script notes that if no access specifier is mentioned, members are private by default, meaning they cannot be accessed directly outside the class.

💡Constructor

Although not explicitly mentioned in the script, the concept of constructors is implied when discussing object creation. A constructor in C++ is a special member function that is called when an object is created, allowing for the initialization of the object's data members.

Highlights

Introduction to the concept of class in C++ and its extension from C structure.

Explanation of how to declare an object to access class members.

Clarification that memory is not allocated upon class declaration but when objects are defined.

Definition of class members and how to access them through member functions.

The default privacy level of class members is private if not specified.

Two methods of defining member functions: inside or outside the class.

Inline member functions should not contain complex expressions or loops.

Syntax and process for defining member functions outside the class.

Class objects require memory allocation, unlike class declaration which is a logical representation.

The difference between logical and physical copies in the context of class variables and objects.

Process of creating class objects and allocating memory for them.

Accessing class members through object references using the dot notation.

Objects as instances of a class, allowing separate data storage for each instance.

The concept of encapsulation in object-oriented programming.

Encapsulation as the binding of data members and member functions into a single unit.

The practical application of encapsulation in managing data within objects.

The importance of objects in object-oriented programming for data management and maintenance.

Transcripts

play00:02

[Music]

play00:09

welcome to nourish 80 this is Kishore

play00:12

and today we are going to continue the

play00:14

oops features in last session we have

play00:18

discussed the feature such as class okay

play00:21

we have discussed the concept of class

play00:24

and how to declare the object today I am

play00:27

going to start what is an object okay

play00:30

when object is created what happens

play00:32

everything okay first of all what is an

play00:35

object okay in C++

play00:39

the new concept is nothing but class

play00:41

actually we have discussed that class is

play00:44

the extension of C structure and here

play00:48

whenever the class is created we are

play00:51

able to store different types of

play00:53

variables and member functions inside of

play00:56

the class now how to define the class

play00:59

members means how to access the class

play01:02

members to access the class members

play01:04

first of all we should have to declare

play01:07

an object from that class okay

play01:10

now what is called an object and why we

play01:13

need the object means to access the

play01:16

class members we should have to define

play01:19

the object why because when class is

play01:22

declared the memory is not allocated

play01:25

okay here the main point is when the

play01:28

class is declared memory is not

play01:31

allocated when objects are defined then

play01:34

only memory is allocated because of

play01:37

classes are the logical representation

play01:40

of okay data that means logical logical

play01:44

means what they are not existed they are

play01:47

not physical okay for example I am going

play01:50

to declare a class like this now class

play01:55

student it is a student is the class

play01:59

name next int ID and character name now

play02:06

these are the data members now here I

play02:10

have not used any access specific that

play02:13

is why by default all the members will

play02:15

become private members okay because of

play02:18

by default the class visibility level or

play02:21

X special

play02:22

is private that is why here whenever the

play02:27

axis specifier is not mentioned

play02:29

automatically your compiler understands

play02:32

their private member's next now to

play02:35

access these members we have to go for

play02:39

member function now public in public

play02:43

area I am going to write like this while

play02:46

you get next why to put now the class is

play02:50

closed and already you have discussed

play02:53

for that we can define the member

play02:55

functions in two ways one is inside the

play02:58

class another one is outside of the

play03:00

class when inside the class it is

play03:02

defined it will become in line member

play03:05

function and here you have to take care

play03:08

what is the clear means in C++ we can

play03:11

define the member functions in two ways

play03:13

one is inside the class another one is

play03:16

outside of the class when the member

play03:18

function is defined inside of the class

play03:21

what happens now it will expand it

play03:24

within in line okay it is expanded as an

play03:27

inline function here we have to take

play03:30

care about we do not have to write a

play03:33

looping statements or complex

play03:35

expressions inside the class that is why

play03:38

here the important point is do not write

play03:41

the looping statements or complex

play03:43

expressions when the definition is

play03:45

conducted inside the class okay next

play03:49

second method is what writing outside

play03:51

means defining the member function

play03:53

outside a class when it is defined

play03:56

outside it does not belongs to our does

play03:58

not becomes to inline member function

play04:01

that is why here we can define the

play04:04

member function in two ways one is

play04:06

inside the class another one is outside

play04:08

the class for example when it is inside

play04:11

the class how it is going to be okay now

play04:13

I am going to give you when the

play04:15

definition is inside what happens

play04:17

suppose it is the get member function I

play04:20

am going to take only one member

play04:23

function that is get here I am going to

play04:25

start like this

play04:27

lawsy out see out enter student ID comma

play04:34

name

play04:36

x-c in ID name now function closed now

play04:42

it is called number function definition

play04:45

inside the class suppose when it is

play04:48

outside how it is going to be now see

play04:51

this I am going to define one more

play04:53

function here brackets closed semicolon

play04:56

and class also closed here watch it it

play05:00

is a member function definition which is

play05:02

conducted inside the class but here put

play05:05

is a another member function which is

play05:08

which is not defined inside the class

play05:11

that means I am going to define this

play05:13

function outside of the class when the

play05:16

member function is defined outside of

play05:17

the class the syntax is like this first

play05:20

return type wide class name class name

play05:24

is what student colon colon next one

play05:29

member function put so here student is

play05:34

the class name and the colon colon is

play05:37

called

play05:37

scope operator and it tells the put

play05:41

belongs to s tu class ok here colon

play05:44

colon is called scope operator and the

play05:47

scope operator represents what the

play05:50

function on it's right side belongs to

play05:53

the class on its left side allow body C

play05:58

out ID equal to ID okay suppose here I

play06:03

am going to close like this next and L

play06:07

later

play06:08

C out name equal to name

play06:13

now first of all ID is going to print

play06:17

here later name is going to print here

play06:19

it is how to define a member function

play06:22

outside of the class and inside the

play06:24

class next now the class is created data

play06:28

members are ready and member functions

play06:30

also ready now the remaining topic is

play06:33

what how to call the member functions or

play06:36

how to access the class members here it

play06:40

is called class declaration and

play06:42

definition now the point is how to

play06:45

access the class members

play06:47

whenever you are going to access the

play06:50

class members first of all we should

play06:52

have to declare the objects okay because

play06:56

of object is the class variable under

play07:00

when the object is declared when the

play07:04

objects are defined then only memory is

play07:07

allocated for example irregularly we are

play07:10

using I and D a comma B comma C

play07:14

generally ABC are called variables which

play07:18

kind of variables means they're integer

play07:21

kind variables are integer type

play07:23

variables now what happens when these

play07:26

variables are created okay how much

play07:29

memory is allocated now six bytes memory

play07:32

allocated and here one point is ABC are

play07:36

integers and here one more integer is

play07:39

there but actually it does not occupy

play07:42

any memory because of it is the logical

play07:46

copy and these are the physical copies

play07:51

that is why here total it needs eight

play07:54

bytes but here what happened only six

play07:57

words memory is allocated because of

play07:59

integer is easier to create the physical

play08:03

copies that is why integer is the

play08:05

logical copy ABC are the physical copy

play08:08

now in our example int is the class and

play08:13

a VCR the objects okay here in our

play08:17

example int is nothing but a class and

play08:20

ABC are the objects according to this

play08:24

explanation objects needs the memory not

play08:27

the class cursor class is a logical

play08:30

representation objects are the physical

play08:33

representation okay now what happens and

play08:36

how to define the object okay now

play08:39

according to this I am going to give the

play08:40

class objects creation say this suppose

play08:44

it is the main function and class name

play08:48

is what s T you now yes here s tu is the

play08:53

class name and s is the object now what

play08:56

happens in stack

play08:59

it is a just a zoom stack here it is the

play09:04

s object now 2 bytes memory allocated

play09:07

for ID and 20 bytes memory allocated for

play09:12

name that is why this class object size

play09:17

is 22 bytes next here another two

play09:22

functions get suppose put now these are

play09:28

the numbers of s object that is why to

play09:31

call the gate input we have to write s

play09:34

dot to get and s dot put it is how to

play09:40

access the numbers thus why discover

play09:42

nothing but calling okay now what

play09:45

happens this gate and put members are

play09:48

going to call okay now when s dot gate

play09:51

is called program automatically goes to

play09:53

get gate is nothing but this function it

play09:57

is going to read the values for ID and

play09:59

name when put is called program goes to

play10:02

put function it is going to put into the

play10:04

ident name that is why here the

play10:07

important point is to access the class

play10:11

members you should have to declare one

play10:14

object at least one object through that

play10:16

object we are able to access the class

play10:19

members it is how to define a class and

play10:23

object according to this okay according

play10:26

to this object means now I am going to

play10:29

explain what is an object according to

play10:32

all these examples first definition of

play10:35

object is it is a class variable R it is

play10:40

a variable of type class it is one of

play10:46

the definition for object it is a

play10:47

variable of type class second one it is

play10:52

a an instance of a class object is also

play10:57

called it is an instance instance means

play11:00

what here copy instance means copy that

play11:04

is why object is also called it is a

play11:06

copy of a class next third one it is

play11:12

the physical representation of a class

play11:18

okay here object is also called what it

play11:22

is the physical representation of a

play11:25

class just before we have discussed when

play11:27

class is declared memory not allocated

play11:30

when the objects are created then only

play11:33

memory that is why objects require the

play11:36

memory that is why it is the physical

play11:37

representation of a class once a class

play11:40

is declared we are able to declare any

play11:43

number of objects from that class for

play11:45

example in previous example I have given

play11:48

student class student class contains

play11:52

what ID name now from the student class

play11:58

I want to declare the object s1 s2 s3

play12:03

now these three are the objects now what

play12:07

happened three memory locations are

play12:09

created one is for s1 another one for s2

play12:14

and another one for s3 and all are

play12:18

having same data members ID ID ID on

play12:23

here name name name here we can store

play12:29

the first student data here second

play12:32

student data here cloud select now

play12:35

it is nothing but object over I entered

play12:38

this one is called object that means

play12:41

each object allows to store the data

play12:43

separately that means each object

play12:46

contains one student data now this data

play12:49

is different from this data this data is

play12:51

different from this data nowadays

play12:54

everything is going on object oriented

play12:57

because of this that is why object

play12:59

oriented programming LoZ to manage the

play13:02

data in the form of objects which is

play13:05

easy to understand okay to maintain the

play13:08

data that is nothing but object oriented

play13:11

that is why object is defined in

play13:14

different ways it is how to define the

play13:17

object next another oops feature is

play13:21

encapsulation now another important

play13:24

feature of oopsies

play13:25

encapsulation now encapsulation is

play13:29

nothing but the process of binding the

play13:31

data members and member functions into a

play13:34

single unit called say this for example

play13:38

here we have declared a class class

play13:40

contains what int ID okay next name now

play13:47

to access this we are going to use wide

play13:51

gate and wide put functions now Watchi

play13:57

here

play13:58

ID and name are the what data members

play14:01

and member functions now what happened

play14:06

class allows the concept of a binding

play14:10

the data members and member functions

play14:12

together into a single unit called class

play14:15

and this feature is called encapsulation

play14:19

okay this which is called encapsulation

play14:21

that is why encapsulation is a process

play14:24

of defining different types of variables

play14:27

and functions into a single unit called

play14:30

class that is nothing but encapsulation

play14:34

okay it is nothing but encapsulation

play14:38

feature in this session we are going to

play14:41

continue the remaining widgets thank you

play14:43

for watching

play14:47

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
C++OOPClassesObjectsEncapsulationProgrammingTutorialData MembersMember FunctionsLogical RepresentationPhysical Copies