SlideShare a Scribd company logo
1 of 20
Rumana Tasnim
Lecture 4
 C++ is a multi-paradigm programming language. Meaning, it
supports different programming styles.
 One of the popular ways to solve a programming problem is
by creating objects, known as object-oriented style of
programming.
 C++ Class
 Before you create an object in C++, you need to define a
class.
 A class is a blueprint for the object.
 We can think of class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows
etc. Based on these descriptions we build the house. House
is the object.
 As, many houses can be made from the same description,
we can create many objects from a class.
 Class: The building block of C++ that leads to Object
Oriented programming is a Class. It is a user defined
data type, which holds its own data members and
member functions, which can be accessed and used
by creating an instance of that class. A class is like a
blueprint for an object.
 For Example: Consider the Class of Cars. There may
be many cars with different names and brand but all
of them will share some common properties like all
of them will have 4 wheels, Speed Limit, Mileage
range etc. So here, Car is the class and wheels, speed
limits, mileage are their properties.
 .
 A Class is a user defined data-type which has data
members and member functions.
 Data members are the data variables and member
functions are the functions used to manipulate these
variables and together these data members and
member functions defines the properties and behavior
of the objects in a Class.
 In the above example of class Car, the data member
will be speed limit, mileage etc and member functions
can be apply brakes, increase speed etc
 An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is
allocated.
 A class is defined in C++ using keyword class
followed by the name of class.
 The body of class is defined inside the curly
brackets and terminated by a semicolon at the end.
 class className
 {
 // some data
 // some functions
 };
 A class is defined in C++ using keyword class
followed by the name of class. The body of class
is defined inside the curly brackets and
terminated by a semicolon at the end.
 Declaring Objects: When a class is defined, only the
specification for the object is defined; no memory or
storage is allocated. To use the data and access
functions defined in the class, you need to create
objects.
 Syntax:
ClassName ObjectName; Accessing data members and
member functions: The data members and member
functions of class can be accessed using the dot(‘.’)
operator with the object. For example if the name of
object is obj and you want to access the member
function with the name printName() then you will have
to write obj.printName() .
 The public data members are also accessed in the
same way given however the private data members
are not allowed to be accessed directly by the
object.
 Accessing a data member depends solely on the
access control of that data member.
This access control is given by Access modifiers in
C++. There are three access modifiers : public,
private and protected.
 class Test
 { private:
 Int data1;
 float data2;
 public:
 void function1()
 { data1 = 2; }
 float function2()
 {
 data2 = 3.5;
 return data2;
 }
 };
 Here, we defined a class named Test.
 This class has two data members: data1 and data2 and two member
functions: function1() and function2()
 The private keyword makes data and
functions private. Private data and functions
can be accessed only from inside the same
class.
 The public keyword makes data and
functions public. Public data and functions
can be accessed out of the class.
 Here, data1 and data2 are private members
where as function1() and function2() are
public members.
 If you try to access private data from outside
of the class, compiler throws error. This
feature in OOP is known as data hiding.
 When class is defined, only the specification
for the object is defined; no memory or
storage is allocated.
 To use the data and access functions defined
in the class, you need to create objects.
Syntax to Define Object in C++
 className objectVariableName; You can
create objects of Test class (defined in above
example) as follows:
 You can access the data members and member
functions by using a . (dot) operator. For
example,
 o2.function1(); This will call the function1()
function inside the Test class for objects o2.
 Similarly, the data member can be accessed as:
 o1.data2 = 5.5; It is important to note that, the
private members can be accessed only from inside
the class.
 So, you can use o2.function1(); from any function
or class in the above example. However, the code
o1.data2 = 5.5; should always be inside the class
Test.
 In this program, two data members data1 and data2 and two
member functions insertIntegerData() and insertFloatData()
are defined under Test class.
 Two objects o1 and o2 of the same class are declared.
 The insertIntegerData() function is called for the o1 object
using:
 o1.insertIntegerData(12); This sets the value of data1 for
object o1 to 12.
 Then, the insertFloatData() function for object o2 is called and
the return value from the function is stored in variable
secondDataOfObject2 using:
 secondDataOfObject2 = o2.insertFloatData(); In this program,
data2 of o1 and data1 of o2 are not used and contains garbage
value.
 You should also check these topics to learn more on objects
 Void-the data that is not returned
 Main function theke class er object create kore sei
object er madhdhome public
function(insertintegerdata) k call kore sei class er
private member e value (int d)assign kora hoy.
 Erpor, float e main function theke user input nei.
Then value return kori
 Erpor float e data return kore main e rakha hoy
 Test-classname
 O1 o2-object name
 Cout-console screen e print f
 Cin-scanf
 cout is an object of the output stream that is
used to show output. Basically, cin is an
input statement while cout is an output
statement. They also use different
operators. cin uses the insertion operator( >>
) while cout uses the extraction operator( <<
).Feb 15, 2018

 endl, among other things, will flush the
stream, i.e., empty it to whatever target it
has (screen, file, ...). So...if you just want to
end the line you can do either. But if you use
cout I suggest you stick with the manipulator
cout since it will also flush your stream. Jan
30, 2009 at 1:33am.Jan 30, 2009
 A constructor is a special type of member
function that initialises an object automatically
when it is created.
 Compiler identifies a given member function is a
constructor by its name and the return type.
 Constructor has the same name as that of the class
and it does not have any return type. Also, the
constructor is always public.
 ... .. ... class temporary { private: int x; float y;
public: // Constructor temporary(): x(5), y(5.5) { //
Body of constructor } ... .. ... }; int main() {
Temporary t1; ... .. ... } Above program shows a
constructor is defined without a return type and
the same name as the class.
 Constructor can be overloaded in a similar way
as function overloading.
 Overloaded constructors have the same name
(name of the class) but different number of
arguments.
 Depending upon the number and type of arguments
passed, specific constructor is called.
 Since, there are multiple constructors present,
argument to the constructor should also be passed
while creating an object.
 For object A1, no argument is passed while creating the
object.
 Thus, the constructor with no argument is invoked
which initialises length to 5 and breadthto 2. Hence,
area of the object A1 will be 10.
 For object A2, 2 and 1 are passed as arguments while
creating the object.
 Thus, the constructor with two arguments is invoked
which initialises length to l (2 in this case)
and breadth to b (1 in this case). Hence, area of the
object A2 will be 2.
 Output
 Default Area when no argument is passed. Area: 10 Area
when (2,1) is passed as argument. Area: 2

More Related Content

What's hot

Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++ThamizhselviKrishnam
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 

What's hot (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
My c++
My c++My c++
My c++
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Object and class
Object and classObject and class
Object and class
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar to C++ Class Fundamentals Explained in 40 Steps

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three noskrismishra
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
Object oriented design
Object oriented designObject oriented design
Object oriented designlykado0dles
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 

Similar to C++ Class Fundamentals Explained in 40 Steps (20)

Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Class and object
Class and objectClass and object
Class and object
 
Object & classes
Object & classes Object & classes
Object & classes
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
C++ classes
C++ classesC++ classes
C++ classes
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Class and object
Class and objectClass and object
Class and object
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
ccc
cccccc
ccc
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Object oriented design
Object oriented designObject oriented design
Object oriented design
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 

Recently uploaded

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 

Recently uploaded (20)

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 

C++ Class Fundamentals Explained in 40 Steps

  • 2.  C++ is a multi-paradigm programming language. Meaning, it supports different programming styles.  One of the popular ways to solve a programming problem is by creating objects, known as object-oriented style of programming.  C++ Class  Before you create an object in C++, you need to define a class.  A class is a blueprint for the object.  We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.  As, many houses can be made from the same description, we can create many objects from a class.
  • 3.  Class: The building block of C++ that leads to Object Oriented programming is a Class. It is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.  For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.  .
  • 4.  A Class is a user defined data-type which has data members and member functions.  Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.  In the above example of class Car, the data member will be speed limit, mileage etc and member functions can be apply brakes, increase speed etc  An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
  • 5.  A class is defined in C++ using keyword class followed by the name of class.  The body of class is defined inside the curly brackets and terminated by a semicolon at the end.  class className  {  // some data  // some functions  };
  • 6.  A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.
  • 7.  Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.  Syntax: ClassName ObjectName; Accessing data members and member functions: The data members and member functions of class can be accessed using the dot(‘.’) operator with the object. For example if the name of object is obj and you want to access the member function with the name printName() then you will have to write obj.printName() .
  • 8.  The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object.  Accessing a data member depends solely on the access control of that data member. This access control is given by Access modifiers in C++. There are three access modifiers : public, private and protected.
  • 9.  class Test  { private:  Int data1;  float data2;  public:  void function1()  { data1 = 2; }  float function2()  {  data2 = 3.5;  return data2;  }  };  Here, we defined a class named Test.  This class has two data members: data1 and data2 and two member functions: function1() and function2()
  • 10.  The private keyword makes data and functions private. Private data and functions can be accessed only from inside the same class.  The public keyword makes data and functions public. Public data and functions can be accessed out of the class.  Here, data1 and data2 are private members where as function1() and function2() are public members.  If you try to access private data from outside of the class, compiler throws error. This feature in OOP is known as data hiding.
  • 11.  When class is defined, only the specification for the object is defined; no memory or storage is allocated.  To use the data and access functions defined in the class, you need to create objects. Syntax to Define Object in C++  className objectVariableName; You can create objects of Test class (defined in above example) as follows:
  • 12.  You can access the data members and member functions by using a . (dot) operator. For example,  o2.function1(); This will call the function1() function inside the Test class for objects o2.  Similarly, the data member can be accessed as:  o1.data2 = 5.5; It is important to note that, the private members can be accessed only from inside the class.  So, you can use o2.function1(); from any function or class in the above example. However, the code o1.data2 = 5.5; should always be inside the class Test.
  • 13.  In this program, two data members data1 and data2 and two member functions insertIntegerData() and insertFloatData() are defined under Test class.  Two objects o1 and o2 of the same class are declared.  The insertIntegerData() function is called for the o1 object using:  o1.insertIntegerData(12); This sets the value of data1 for object o1 to 12.  Then, the insertFloatData() function for object o2 is called and the return value from the function is stored in variable secondDataOfObject2 using:  secondDataOfObject2 = o2.insertFloatData(); In this program, data2 of o1 and data1 of o2 are not used and contains garbage value.  You should also check these topics to learn more on objects
  • 14.  Void-the data that is not returned  Main function theke class er object create kore sei object er madhdhome public function(insertintegerdata) k call kore sei class er private member e value (int d)assign kora hoy.  Erpor, float e main function theke user input nei. Then value return kori  Erpor float e data return kore main e rakha hoy  Test-classname  O1 o2-object name  Cout-console screen e print f  Cin-scanf
  • 15.
  • 16.  cout is an object of the output stream that is used to show output. Basically, cin is an input statement while cout is an output statement. They also use different operators. cin uses the insertion operator( >> ) while cout uses the extraction operator( << ).Feb 15, 2018 
  • 17.  endl, among other things, will flush the stream, i.e., empty it to whatever target it has (screen, file, ...). So...if you just want to end the line you can do either. But if you use cout I suggest you stick with the manipulator cout since it will also flush your stream. Jan 30, 2009 at 1:33am.Jan 30, 2009
  • 18.  A constructor is a special type of member function that initialises an object automatically when it is created.  Compiler identifies a given member function is a constructor by its name and the return type.  Constructor has the same name as that of the class and it does not have any return type. Also, the constructor is always public.  ... .. ... class temporary { private: int x; float y; public: // Constructor temporary(): x(5), y(5.5) { // Body of constructor } ... .. ... }; int main() { Temporary t1; ... .. ... } Above program shows a constructor is defined without a return type and the same name as the class.
  • 19.  Constructor can be overloaded in a similar way as function overloading.  Overloaded constructors have the same name (name of the class) but different number of arguments.  Depending upon the number and type of arguments passed, specific constructor is called.  Since, there are multiple constructors present, argument to the constructor should also be passed while creating an object.
  • 20.  For object A1, no argument is passed while creating the object.  Thus, the constructor with no argument is invoked which initialises length to 5 and breadthto 2. Hence, area of the object A1 will be 10.  For object A2, 2 and 1 are passed as arguments while creating the object.  Thus, the constructor with two arguments is invoked which initialises length to l (2 in this case) and breadth to b (1 in this case). Hence, area of the object A2 will be 2.  Output  Default Area when no argument is passed. Area: 10 Area when (2,1) is passed as argument. Area: 2