Lecture 6- Classes and Objects in C++

IIT Roorkee July 201
61 followers
Jan 2, 2025
[Music]
[Music]
so welcome to lecture six classes and
objects so we briefly Define how we can
create the classes in C++ all right so
we have also studied it is nothing but
the blueprints for creating objects all
right so once you create a class right
assume that you're creating a class so
it uh encapsulates the data all right so
in fact data that means attributes and
the behavior which is called functions
or methods all right so in a set
notation all right so we can Define this
as a tuple all right so here it is
nothing but a tuple C is equal to a
comma M where a we call it as a set of
attributes suppose you are using the
data right you call it as a member data
and M is nothing but a member function
or you call it as
methods so we have already talked about
how the syntax Works in C++ so when I
define a class right so here you go you
will write the keyword called class
right so we'll have a class
keyword and then the name of the class
you are opening the braces the code
block is starting over here and you have
the member data called
attributes and you will have the
functions you can have several
attributes you can use arrays also all
right and you can have several methods
several functions right you call it as a
member data or member variable or member
function and then you will have the end
parenthesis with a semicolon in the case
of C++ you should have the semicolon
this is how we can define a class in C++
so what is the motivation behind all
right for class design in
C++ so when we are writing
objectoriented programming so as we had
seen it is mainly based on the objects
and classes right which is a very
powerful Paradigm all right so this can
solve the wide variety of problems so
you can have the list of problems over
here so the list of problems when you
are having a class you can have a object
right so class and from the class you
can create the object and we can use
some of the objectoriented programming
Concepts like uh inheritance
polymorphism
encapsulation Etc right so all these can
be
effectively applied right we can apply
so now we had seen right in the few
classes back we had seen the tangible
objects and intangible objects so for
example
right so intangible objects assume that
I want to create a class or I have to
solve the problem using objectoriented
programming for Library Management
Systems so when you have Library
Management Systems you can think of
right books users all right loans staff
members right so these you are model
mean you are modeling these as classes
right you're modeling these as classes
like then we have attributes for example
book title all right due dates and
behavior like borrowing or returning the
books all right so the attributes I
Define book title you call it as a
member data then we can have what are
all the due dates and then I if I want
to write certain functions like uh
borrowing or returning the books right
so this is how I can think of the
blueprint for the library management
system similarly banking system all
right so classes can represent uh
customers accounts transactions
all right your method can include the
deposit withdrawal and balance in query
so when the intangible objects like
banking systems so how I can have a
class so from the class right I mean I
can uh represent the customers account
and transactions and then I will have
set of member functions all right so
like uh deposit withdrawal balance
inquiry Etc right so this is how I mean
I can think of writing a object object
oriented code for these kind of problems
similarly suppose you are interested in
let us say gaming the is game all right
so I can think of classes for pieces
like Pawn King Rook Queen right so
because they have a specific movements
and behaviors all right and also I can
think of the board right the board can
be an object so that can have all the
pieces so when you're writing any games
right the chess is one of the games so
if you want to have right uh other board
games or any other games you can think
of in object oriented way similarly
traffic simulations all right so we can
think of vehicles roads traffic lights
so these can be objects right so when
I'm talking about the classes of this
problem all right so the classes can
Define the behavior of every component
right of this traffic simulation right
and the method simulate movement and
traffic flow right so when I'm thinking
about uh traffic simulation so I can
write a program using objectoriented
programming similarly when you are going
for the data structures like a link list
Stacks cues and trees all right so once
you are done with letter say C++ and
Java you can think of next one is a data
structure and advanced data structure so
I can define a class node right class
link list class tack class cues class
binary search trees class aval trees
right so these are all nothing but the
classes and you can have right several
methods insertion right or addition of
the node removal of the node or I want
to manipulate certain data so these are
all coming under the methods right so
you can think of whenever I have a link
list or any data structure so the data
structure can be a class and then the
several operations you can have several
operations those operations are nothing
but the methods or member functions
right similarly we are in the artificial
intelligence Paradigm right in neural
networks we can use objectoriented
programming like uh layers neurons the
connections right so these are all can
be considered as a classes and then when
I'm talking about the methods you have a
forward propagation backward propagation
and you are to train the model correct
so when I want to have objectoriented
Concept in neural network so you can
think of so the main idea of all these
problems that I have suggested all right
so you have a class so from the class
you can have the objects right so once
you have the objects let us assume
inside the class you can have set of
member data you can use any data it may
be integer or double or float or even
array of integers all right you can
think of these are all the member data
and several member functions right when
I'm talking about the data structure I
have to write a method for insertion
deletion
update pop operation in Stacks peep
operation in stack push operation in
stack so all these operations when I'm
talking about the operations they are
nothing but the methods okay so this is
how I can think of using the object
oriented programming how I can solve
these kind of problems so similar to
class design in C++ so we have class
design in Java all right so when we talk
about the class
design in Java so here you go all right
so you have name of the class so here
you have name of the class right the
class name and then you have attributes
and methods right attributes is nothing
but data member so in Java you call it
as data member and then you can have set
of methods and methods are nothing but
the behaviors or operations or the
functions that you are calling in C++
you call this in Java so the class is a
keyword and you can have a name of the
class you can have a attribute you can
have a method so only thing after Braes
you should not have a semicolon okay so
this is how we can Define the class in
Java so now you know how to create a
class how to define a object right or
how to declare a object or I can say
that how to instantiate an objects right
instantiate an object or instantiate the
objects right so this you know that so
with this we will see some of the
problems okay so now write a C++ program
to multiply two complex numbers using
class and objects all right so you have
to multiply two complex numbers so how
do you define a complex number so C is
equal to let us say one let us say C1 A1
+ ib1 so this is one complex number and
C2 equal to A2 + I B2 right when I
multiply C1 into C2 it is nothing but A1
+ I B1 multiplied by A2 + I B2 correct
so which is nothing but A1 into A2 i s
is minus1 therefore I'll get minus B1
into B2 correct plus I
into B1 A2 plus A1 B2 or I can write B2
A1 all right so the multiplication when
I am doing the product right so I have
to get this as the output all right so
these two numbers will be the input so
it is a pair the pair will be the input
and the program has to multiply these
two complex numbers and I have to get
the output like this so now the user
will give you the numbers A1 B1 the pair
A2 B2 the pair and the resultant I have
to get this right so how we are going to
solve this using object oriented
programming so let us consider this
program all right so let us have the
class complex right I'm creating a class
complex the idea is we have to multiply
these two complex numbers right I'm
creating a complex class complex so when
I write c equal to A+ I right this a is
a real value and B here is I into B we
can call it as a complex imaginary value
right it's a imaginary value so
therefore we have float real and Float
imaginary is a member data so you have
two member data the member data are
float real and Float imaginary next you
have set of member functions all right
you have member function called set
values usually we see this getter and
Setter function so set values so you
have two parameters float R and Float I
all right so whatever you are setting
the value or taking the value from the
user real and imaginary all right so
here R will be assigned to real and I
will be assigned to imaginary right
sooner or later we'll talk about the
access modifiers the private and public
all right so right now still it is a
keyword I have not explain so we will
once we talk about we'll again see the
complete program so right now no need to
worry about the private and public so
that is the reason here R will be
assigned to real and I will be assigned
to imaginary right so I can access this
inside the class that is the meaning so
when I taking the input from the user
right R and I so B based on this
function so when I'm passing these
values in the main so now R and I you
will have the value so that will be
assigned to real and imaginary so this
is one function and then you have
another function called multiply all
right so here we use the keyword static
I'll explain about static maybe we'll
see one or two examples all right how we
are defining the keyword static all
right so now complex multiply right so
here you have the multiply function
whose return type is the right the
return type is the object right your
return type is the class complex all
right your return type is class complex
so I will take complex address C1 and
another one is complex address C2 all
right so suppose I have if you would
have done the reference operator int
address C1 all right assume that I have
int let us say C2 equal to 10 right so
when I write int C2 equal to 10 so we
know that let us say this will be stored
in some address assume that the address
is having let us assume this is your
memory location all right so this is C2
taking the value 10 all right so now if
I put in address C1 which is equal to C2
right so now if you look what is address
of C2 address of C2 is 42 address of C2
is 42 correct so now when I write like
this int address C1 equal to C2 the
meaning is address C1 is also pointing
to the location 42 all right so that is
the meaning of this because we are using
this syntax over here all right so maybe
uh the syntax go like this complex when
we are calling the function so you will
give like this some let us say C11 all
right so the meaning is address C1 and
address C11 both are same right when
you're are doing like this address C1
and address C11 both are same address of
C1 and address of C11 both are same all
right so with this in mind we'll again
come back when we are calling this
particular function in line number 17
all right so this concept will be useful
to you so now as we uh worked out right
how to multiply two complex numbers
right maybe one more time I'll do it A1
+ ib1 because here we have set of
Expressions right into A2 + ib2 so we
know that it is A1 A2 minus B1 B2 I squ
is min-1 right this will be your real +
I into A1 B2 + A2 B1 right so now now if
you look this is a resultant right the
resultant complex number let us say C3
so C3 will be it will be like a plus IB
form right C3 will be some X Plus iy
form right what is X that is real so X
will be let us say C3 is equal to x + i
y all right in that case X is equal to
A1 A2 minus B1 B2 and Y is equal to A1
B2 + A2 B1
right so this you call it as result.
real X is nothing but your result do
real so you can see that C1 do real into
C2 do real which is nothing but A1 into
A2 all right and then you have minus you
have minus B1 into B2 C1 do imaginary
into C2 do imaginary B1 B2 and imaginary
C1 do real A1 into C2 do imaginary B2
right similarly C1 do imaginary into C2
do real all right so that you have
evaluated this and then return result
all right so result is a complex number
you can see the return type is complex
right so this is one main function so
you are another member function over
here and the last member function is you
are displaying all right you are
displaying real and imaginary all right
so if imaginary is greater than or equal
to zero you display a plus IB otherwise
a minus IB so that is the reason that's
why it's been taken like this right so
basically you are displaying A+ IB or X
Plus iy form okay so up to here we have
return the class correct so this is how
we Define the class called complex so
you have real and imaginary data members
or member data and then you have member
functions called set values multiply and
then display so now come to the main
program so in the main program we are
creating two objects C1 and C2 and then
you need another object called result
right so you obviously require result is
equal to C1 star C2 where C1 C2 are the
complex number so result the third
object all right so now you will have C1
is nothing but A1 Plus ib1 so you need
real one and imaginary one and C2 is A2
plus ib2 so real 2 and imaginary 2 so
take the input from the user for for
example I write 2 + 3 I and then another
one is 4 + 5 I all right in that case I
have to write C in real one C in
imaginary one all right and then you
will have C1 do set value so now you are
calling the object C1 you have already
defined over here so the object C1 is
invoking the member function the member
function called set values using the dot
operator so you have the object the
object is invoking using dot operator
and one of the member function called
set values right you are pausing real
one and imaginary one right you are
pausing real one and imaginary one so
let us say what is happening in the set
values right so here you have set values
assume that I'm passing two and three
all right so your real is two and
imaginary will be three right for the
object C1 your real is two and imaginary
is three so this is a meaning and then
similarly C2 is invoking right you
taking two more input real two and
imaginary 2 right so this is your real
two this is your imaginary 2 all right
so now C2 is invoking set values member
function right you have a DOT operator
and then you are pausing real two and
imaginary 2 assume that I'm pausing four
and five correct so when I'm passing
four and five so what I will get my R
real will be four imagine will be five
right C2 do real will be four C2 do
imaginary will be five so this is the
meaning all right so finally all right
so here under the class complex all
right you have scope resolution operator
all right and then you are having
multiply C1 comma C2 so this means I can
write this function outside the class
all right so when I'm writing this
correct in the main all right so I can
do that so that means I am calling this
particular function multiply all right
I'm calling this function
multiply correct with passing two
arguments C1 and C2 all right so when
I'm calling multiply let us see what is
happening so here you go multiply so
complex address C1 which is equal to
C1 C1 that you're passing from here
right C1 do real C1 do imaginary
similarly complex C2 which is equal to
C2 all right so that means address C1
and add C2 so here your C1 over here
right you have an object C1 in the main
all right so you have object C1 in the
main object C2 is also in the main
correct so which is you are creating an
object with some address value right so
for example I create C1 whose address is
let us say 42 address of C1 and I create
C2 whose address is 46 so once I have
this when I'm calling this your address
see 1 equal to C1 so that means C1 when
I Define inside the function C2 I Define
inside the function both will have the
same address what you have defined
outside the class that is the meaning
okay so now rest is fine see1 C2 you
pass that means C1 real C2 imaginary C1
imaginary right and C2 real all you have
passed and then this computation will be
done so once this computation will be
done we know know that right so the
complex multiplication is happening and
then you are displaying the result so
you're getting a result all right your
result is the object whose class is
complex right so therefore you have a
complex class this is called scope
resolution
operator and then you're calling the
multiply function by pausing two
arguments C1 and C2 right right so
multiplication will be done when you're
calling this function and result is
invoking so result of multiplication
this is invoking display all right so
that means result do real and result do
imaginary will be
displayed right so this is what exactly
happening all right so result do real
and result do imaginary will be
displayed for example assume that we are
giving 2 three right 2 + 3 I the same
right 2 + 3 3 I as a one input and
another input is 4 + 5 I correct so when
I multiply these 2 what I will get so I
should get - 7 + 22 I this is obvious
right 2 into 4 is 8 3 into 5 is 15 I 2 -
15 + I into 3 into 4 is 12 and 5 into 2
is 10 so 8 - 15 is - 7 + 12 + 10 is 22 I
so I should get this is output in fact
so this is giving the correct output
right so this is how we are doing the
complex multiplication using class and
objects all right so what exactly we
have done so we have created a class
right so the class is a complex the name
of the class is complex and then you
have member data so real and imaginary
or member data and then you have set of
member functions called set values all
right and then you have multiply this is
a main function right main member
function called
multiplication and then displaying all
right so you can display maybe the
number one C1 complex one A1 Plus ib1
complex 2 A2 plus ib2 so this you can
display all right and then result so
once you are doing the complex
multiplication all right so once you're
doing a complex multiplication so the
resultant can also be displayed using
this so that means whenever you are
creating an object you have member data
and member function so here we have used
two member data and then three member
functions so up to here line number 31
you have class so once you have a class
when you go to the main program you are
creating objects all right so C1 C2
result these are all the objects so you
know how to create a class and now you
are instantiating an object or
instantiating objects so the objects are
nothing but C1 C2 and result right so
you have the complex class and then the
usual float real one imaginary one real
two imaginary 2 right so usual data type
so that you have used in your procedural
oriented programming all right so then
you're taking the input from the user so
when you're taking an input from the
user the set values will set all right
where C1 is invoking set value C2 is
invoking set value and it is nothing but
you have a1+ ib1 and A2 plus ib2 and
then you are calling the multiplication
function so here I am using the
reference operator so the reference
operator C1 when you define the object
in the main C1 you are defining object
inside the class so both are pointing
out the same location all right so this
is how we have seen the concept of that
integer right integer address a or
integer address C1 equal to some C11 so
both C1 and C11 pointing to the same
address correct and then when it is uh
multiply is invoking right in fact the
multiply when you are calling this
function with the C1 and C2 passing the
arguments so these two will be multiply
C1 and C2 will be multiply and the
resultant will be over here right you're
calling the multiplication member
function so it will throw the result so
result is of the form X x + i y complex
number so that we are displaying so we
have given the sample test 2 + 3 I and 4
+ 5i so and then we got the exact output
all right so this is how we can have a
class and then create an object and uh
you can solve as I said several problems
this is one of the mathematical problems
right similarly you can think of right
the banking system in fact you can think
of neural networks or or we will go
right how to solve the problems like
data structures right create a class let
us say link list or even the arrays all
right so create a class arrays and then
you can do set of operations like uh
insertion in arrays deletion in arrays
updating the arrays or you can write the
program like a searching you can write a
function like searching member function
like searching sorting all right so the
main idea is so with the help of object
oriented programming right so you all
might have studied procedural oriented
programming you can think of right so
creating a class right defining a class
and creating an object and then all
right so you can invoke many functions
all right so here we had seen several
keywords right so one of the keywords
you are seeing over here we'll talk
about private and public slightly later
and here static all right so in the next
class we'll see few examples about
static all right and then right how the
static Works without static variable or
with static variable how the results are
being changed so that we will see in the
next class thank you
a
[Music]
