SlideShare a Scribd company logo
1 of 20
Lecture 3
CSE1201: Object Oriented
Programming-I (C++)
9/5/2023 2
Paradigm: Multi-paradigm
Creator: Bjarne Stroustrup
First appeared: 1983
C++ Program Execution Flow
9/5/2023 3
A First C++ Program
#include <iostream>
using namespace std;
// main() is where program execution begins
int main() {
// prints Hello World
cout << "Hello World";
return 0;
}
9/5/2023 4
A First C++ Program
• Save the program as first.cpp
• Build and Run the code
9/5/2023 5
Another C++ Program
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
cout << "number: " <<num << endl;
return 0;
}
9/5/2023 6
The General Form of a Class in C++
 A class is defined by specifying the data and the
code that operate on the data
 The general form:
class classname{
private data and functions declarations
access-specifier:
data and functions declarations
access-specifier:
data and functions declarations
};
7
9/5/2023
Example
class person
{
int age;
public:
void display_age()
{
cout << age;
}
};
The General Form of a Class in C++
• Functions that are declared within a class are
called member functions
• Member functions may access any element of the
class of which they are a part, this includes all
private elements
• Variables that are elements of a class are called
member variables or data members. (The term
instance variable is also used)
9
9/5/2023
Access specifier
• access-specifier is one of these three C++ keywords:
– public
– private
– Protected
• (access-specifier is also known as visibility label)
• By default, functions and data declared within a class
are private to that class and may be accessed only by
other members of the class
9/5/2023 10
Access specifier
 The public access-specifier allows functions or data
to be accessible to other parts of your program
 The protected access-specifier is needed only when
inheritance is involved
 Once an access-specifier has been used, it remains in
effect until either another access-specifier is
encountered or the end of the class declaration is
reached
11
Access specifier
• In general, you should make all data members of a
class private to that class.
– This is part of the way that encapsulation is achieved
• However, there may be situations in which you will
need to make one or more variables public
12
Creating Objects
 Once a class has been declared, we can create
variables (object) of that type by using the class
name (like any other built in variables)
 The necessary memory space is allocated to an
object at this stage
 Class specification provides only a template and
does not create any memory space for the members
 Format: class_name object_name;
 Example: person p;
13
9/5/2023
Creating Objects
 Objects can also be created when a class is defined
by placing their names immediately after the closing
brace.
class class_name
{
} x, y, z;
 Each object contains its own copy of each instance
variables defined by the class
14
9/5/2023
Accessing class members
 The private data of a class can only be
accessed only through the member functions
of that class
 Following is the format for accessing a
member
Object_name.function_name(arguments)
Object_name.variable_name
9/5/2023 15
Defining member functions
 Member functions can be defined in two
places:
 Outside the class definition
 Inside the class definition
 Irrespective of the place of definition, the
function will perform the same task
9/5/2023 16
Outside the class definition
 Member functions that are only declared inside of a
class, have to be defined separately outside the class
 General format of a member function definition outside
the class is
return_type class_name:: function_name (arguments)
{
}
 The membership label class_name:: tells the compiler
that, function function_name belongs to the class
class_name
 The symbol :: is called the scope resolution operator
9/5/2023 17
Inside the class definition
 Another method of defining a member function is to
replace the function declaration by the actual
function definition inside the class definition
 Usually, only small functions are defined inside the
class definition
9/5/2023 18
General structure of a c++ program
Include files
Class declarations
Member functions definitions
Main function program
9/5/2023 19
Thank you

More Related Content

Similar to lecture3.pptx

Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopmShweta Shah
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectsecondakay
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 

Similar to lecture3.pptx (20)

Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
My c++
My c++My c++
My c++
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class and object
Class and objectClass and object
Class and object
 
Class and object
Class and objectClass and object
Class and object
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Object & classes
Object & classes Object & classes
Object & classes
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
Jscript part2
Jscript part2Jscript part2
Jscript part2
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 

Recently uploaded

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
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
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
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
 
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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
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
 
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
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
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
 
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
 

Recently uploaded (20)

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
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
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
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
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
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 - 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...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
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
 
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...
 

lecture3.pptx

  • 1. Lecture 3 CSE1201: Object Oriented Programming-I (C++)
  • 2. 9/5/2023 2 Paradigm: Multi-paradigm Creator: Bjarne Stroustrup First appeared: 1983
  • 3. C++ Program Execution Flow 9/5/2023 3
  • 4. A First C++ Program #include <iostream> using namespace std; // main() is where program execution begins int main() { // prints Hello World cout << "Hello World"; return 0; } 9/5/2023 4
  • 5. A First C++ Program • Save the program as first.cpp • Build and Run the code 9/5/2023 5
  • 6. Another C++ Program #include <iostream> using namespace std; int main() { int num; cin >> num; cout << "number: " <<num << endl; return 0; } 9/5/2023 6
  • 7. The General Form of a Class in C++  A class is defined by specifying the data and the code that operate on the data  The general form: class classname{ private data and functions declarations access-specifier: data and functions declarations access-specifier: data and functions declarations }; 7 9/5/2023
  • 8. Example class person { int age; public: void display_age() { cout << age; } };
  • 9. The General Form of a Class in C++ • Functions that are declared within a class are called member functions • Member functions may access any element of the class of which they are a part, this includes all private elements • Variables that are elements of a class are called member variables or data members. (The term instance variable is also used) 9 9/5/2023
  • 10. Access specifier • access-specifier is one of these three C++ keywords: – public – private – Protected • (access-specifier is also known as visibility label) • By default, functions and data declared within a class are private to that class and may be accessed only by other members of the class 9/5/2023 10
  • 11. Access specifier  The public access-specifier allows functions or data to be accessible to other parts of your program  The protected access-specifier is needed only when inheritance is involved  Once an access-specifier has been used, it remains in effect until either another access-specifier is encountered or the end of the class declaration is reached 11
  • 12. Access specifier • In general, you should make all data members of a class private to that class. – This is part of the way that encapsulation is achieved • However, there may be situations in which you will need to make one or more variables public 12
  • 13. Creating Objects  Once a class has been declared, we can create variables (object) of that type by using the class name (like any other built in variables)  The necessary memory space is allocated to an object at this stage  Class specification provides only a template and does not create any memory space for the members  Format: class_name object_name;  Example: person p; 13 9/5/2023
  • 14. Creating Objects  Objects can also be created when a class is defined by placing their names immediately after the closing brace. class class_name { } x, y, z;  Each object contains its own copy of each instance variables defined by the class 14 9/5/2023
  • 15. Accessing class members  The private data of a class can only be accessed only through the member functions of that class  Following is the format for accessing a member Object_name.function_name(arguments) Object_name.variable_name 9/5/2023 15
  • 16. Defining member functions  Member functions can be defined in two places:  Outside the class definition  Inside the class definition  Irrespective of the place of definition, the function will perform the same task 9/5/2023 16
  • 17. Outside the class definition  Member functions that are only declared inside of a class, have to be defined separately outside the class  General format of a member function definition outside the class is return_type class_name:: function_name (arguments) { }  The membership label class_name:: tells the compiler that, function function_name belongs to the class class_name  The symbol :: is called the scope resolution operator 9/5/2023 17
  • 18. Inside the class definition  Another method of defining a member function is to replace the function declaration by the actual function definition inside the class definition  Usually, only small functions are defined inside the class definition 9/5/2023 18
  • 19. General structure of a c++ program Include files Class declarations Member functions definitions Main function program 9/5/2023 19