SlideShare a Scribd company logo
UNIT - 2UNIT - 2
Mrs. Pranali P. Chaudhari
Contents
Namespace
Data Types
Reference Variables
this pointerthis pointer
Classes and Objects
Access Specifiers
Defining data members and member functions
Array of Objects
Namespace
Namespace defines a scope for the identifiers that are
used in the program.
Std is a namespace where ANSI C++ standard class
libraries are defined.
We can define our own namespace in our program.We can define our own namespace in our program.
Syntax:
namespace namespace_name
{
// declaration of variables, functions, classes etc.
}
Namespace
Example:
namespace Testspace
{
int m;
void display (int n)void display (int n)
{
cout<< n;
}
}
To assign value to m we use scope resolution operator as,
Testspace :: m = 100 ;
Tokens
Tokens: the smallest individual units in a program are
known as tokens.
Keywords ( int, char, new, delete, etc…..)Keywords ( int, char, new, delete, etc…..)
Identifiers ( a, display, etc…..)
Constants
Strings
Operators
Data Types in C++
C++ Data Types
User Defined types
Structure
Build – in types Derived Types
ArrayStructure
Union
Class
Enumeration
Array
Function
Pointer
Reference
Integral Type Void Floating Type
int Char float double
Enumerated Data Type
It is a user defined data type which provides a way for
attaching names to numbers.
For eg: enum shape {circle, square, triangle}For eg: enum shape {circle, square, triangle}
enum color {red, blue, green}
In C++ the tag name becomes new data type.
For eg: shape ellipse;
Quiz 1
What is the difference between a variable and
constant?
The difference between variables and constants is that
variables can change their value at any time but
constants can never change their value.
Variables
C++ allows declaration of variables anywhere in the
scope.
For eg: float average;For eg: float average;
average = sum/i ;
C++ permits initialization of the variables at run time.
// Dynamic initialization
For eg: float average =sum/i ;
Reference Variables
C++ defines a reference variable that provide an alias
for a previously defined variable.
Syntax:Syntax:
data- type &reference-name = variable-name
It is used to passing arguments to functions
For eg: float total = 100;
float & sum = total;
Reference Variable
Major application of reference variables is in passing
arguments to functions.
void f (int & x) // uses reference
{{
x = x + 10; // x is incremented so also m
}
int main()
{
int m = 10;
f (m); // function call
}
Operators in C++
C++ has a rich set of operator:
Insertion Operator <<
Extraction Operator >>
Scope Resolution Operator ::
Pointer-to-member declarator ::*Pointer-to-member declarator ::*
Pointer-to-member operator ->*
Pointer-to-member operator .*
Memory release operator delete
Memory allocation operator new
Line feed operator endlendl
Field width operator setwsetw
Scope Resolution Operator
Scope resolution operator allows access to global version of
a variable.
For eg:
::m will always refer to the global m.::m will always refer to the global m.
Major application of the scope resolution operator is in
classes to identify the class to which a member function
belongs.
For eg:
void employee :: getdata(void)
int item :: count
Memory Management Operator
C++ defines new and delete operators for allocating and freeing the
memory .
An object is created by using new, and destroyed by using delete as
and when required.
Syntax:
pointer-variable = new data-type;
int *p = new int; *p = 25;
pointer-variable = new data-type(value);
int *p = new int(25);
pointer-variable = new data-type[size]
int *p = new int [10];
delete pointer-variable;
delete p;
Manipulators
Manipulators are the operators that are used to format
the data display.
Commonly used manipulators are endl and setw.Commonly used manipulators are endl and setw.
endl manipulator causes a line feed to be inserted.
setw specifies a field width for printing the value of the
variable.
Type Cast Operator
C++ permits explicit type conversion of variables using
type cast operator.
average = sum / (float) i ; // C notationaverage = sum / (float) i ; // C notation
average = sum / float (i) ; // C ++ notation
A type name behaves as if it is a function for
converting values to a designated type.
this pointer
C++ uses a unique keyword this to represent an object
that invoke a member function.
this is a pointer that points to the object for which thisthis is a pointer that points to the object for which this
function was called.
For example:
A call to a function A.max() will set the pointer this to
the address of the object A.
Quiz 2
Define Class.
A class is a collection of objects of similar type.
What are the contents of class?
The class consists of data members and member
functions. Both of which characterise the object
for which the class is defined.
Classes and Objects
A class is a way to bind the data and its associated functions
together.
It allows the data to be hidden, if necessary, from external
use.use.
When defining a class a new ADT is created that can be used
like any other built-in type.
A class have two parts:
Class declaration
Class function definitions
Class Declaration
General form of class declaration:
class class_name
{
private :private :
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Class Example
class item
{
int number;
float cost;float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
Quiz 3
What are Access Specifiers?
Access modifiers (or access specifiers)
are keywords in object-oriented languages that set
the accessibility of classes, methods, and other
members.
Accessing Class Members
We have three basic access types:
private
public
protected
Private members can be accessed within the class itself.
Public members can be accessed from outside the class.
A protected access specifier is a stage between private
and public access. If member functions defined in a class
are protected, they cannot be accessed from outside the
class but can be accessed from the derived class.
Accessing Class Members
The main() cannot contain the statements that access
private data members directly.
They use the member function to use them.
For eg:
x.getdata (100, 75.5)x.getdata (100, 75.5)
x.number = 100; // illegal
Objects communicate by sending and receiving
messages.
For eg:
x.putdata();
Defining Member Functions
Member functions can be defined in two phases:
Outside the class definition
Inside the class definition
A member function can also be private
A private member function can only be called by
another function that is a member of its class.
Static Data Members
A data member of a class can be static.
A static member variable has the following characteristics:
It is initialized to zero when the first object of its class isIt is initialized to zero when the first object of its class is
created. No other initialization is permitted.
Only one copy of that member is created for the entire class.
It is visible only within the class
Example
Static Members Functions
A static member function has following characteristics:
A static function can have access to only static members
(functions or variables) declared in the same class.(functions or variables) declared in the same class.
A static member function can be called using the class
name as:
class-name :: function-name;
Eg: item :: showcount();
Example
Quiz 4
Define Array.
Array is a data structure used to store the elementsArray is a data structure used to store the elements
of same type.
Array of Objects
Similar to structure we can create a array of objects for a
class.
For eg:
employee manager[5];
employee worker[10];
Example
Summary
________ defines a scope for the identifiers that are used
in the program.
Main purpose of defining reference variable is ______.
C++ defines ______ and _____ operators for allocating
and freeing the memory .and freeing the memory .
The main() cannot contain the statements that access
private data members directly. (True/False)
A private member function can only be called by
another function that is a member of its class.
(True/False)
A static member function can be called using ______ .
References
Object Oriented Programming with C++ by E.
Balagurusamy.Balagurusamy.
Introduction to C++
Ad

More Related Content

What's hot (20)

Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
structure and union
structure and unionstructure and union
structure and union
student
 
Basic c#
Basic c#Basic c#
Basic c#
kishore4268
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
Jussi Pohjolainen
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
HalaiHansaika
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
guestf0562b
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
Gamindu Udayanga
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Prof. Dr. K. Adisesha
 
Structures
StructuresStructures
Structures
archikabhatia
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
ramya marichamy
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
structure and union
structure and unionstructure and union
structure and union
student
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
Jussi Pohjolainen
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
guestf0562b
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
Gamindu Udayanga
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
ramya marichamy
 

Similar to Introduction to C++ (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
USER DEFINE FUNCTION AND STRUCTURE AND UNION
USER DEFINE FUNCTION AND STRUCTURE AND UNIONUSER DEFINE FUNCTION AND STRUCTURE AND UNION
USER DEFINE FUNCTION AND STRUCTURE AND UNION
MSridhar18
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment  - 23MTS5730.pptxC++ tutorial assignment  - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
Mufaddal Nullwala
 
My c++
My c++My c++
My c++
snathick
 
C++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHatC++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
jatin batra
 
02.adt
02.adt02.adt
02.adt
Aditya Asmara
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
InfinityWorld3
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
Sowri Rajan
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
USER DEFINE FUNCTION AND STRUCTURE AND UNION
USER DEFINE FUNCTION AND STRUCTURE AND UNIONUSER DEFINE FUNCTION AND STRUCTURE AND UNION
USER DEFINE FUNCTION AND STRUCTURE AND UNION
MSridhar18
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
C++ tutorial assignment - 23MTS5730.pptx
C++ tutorial assignment  - 23MTS5730.pptxC++ tutorial assignment  - 23MTS5730.pptx
C++ tutorial assignment - 23MTS5730.pptx
sp1312004
 
C++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHatC++ Interview Questions and Answers PDF By ScholarHat
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
jatin batra
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Ad

More from Pranali Chaudhari (6)

Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
Files and streams
Files and streamsFiles and streams
Files and streams
Pranali Chaudhari
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
Pranali Chaudhari
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
Pranali Chaudhari
 
Ad

Recently uploaded (20)

CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 

Introduction to C++

  • 1. UNIT - 2UNIT - 2 Mrs. Pranali P. Chaudhari
  • 2. Contents Namespace Data Types Reference Variables this pointerthis pointer Classes and Objects Access Specifiers Defining data members and member functions Array of Objects
  • 3. Namespace Namespace defines a scope for the identifiers that are used in the program. Std is a namespace where ANSI C++ standard class libraries are defined. We can define our own namespace in our program.We can define our own namespace in our program. Syntax: namespace namespace_name { // declaration of variables, functions, classes etc. }
  • 4. Namespace Example: namespace Testspace { int m; void display (int n)void display (int n) { cout<< n; } } To assign value to m we use scope resolution operator as, Testspace :: m = 100 ;
  • 5. Tokens Tokens: the smallest individual units in a program are known as tokens. Keywords ( int, char, new, delete, etc…..)Keywords ( int, char, new, delete, etc…..) Identifiers ( a, display, etc…..) Constants Strings Operators
  • 6. Data Types in C++ C++ Data Types User Defined types Structure Build – in types Derived Types ArrayStructure Union Class Enumeration Array Function Pointer Reference Integral Type Void Floating Type int Char float double
  • 7. Enumerated Data Type It is a user defined data type which provides a way for attaching names to numbers. For eg: enum shape {circle, square, triangle}For eg: enum shape {circle, square, triangle} enum color {red, blue, green} In C++ the tag name becomes new data type. For eg: shape ellipse;
  • 8. Quiz 1 What is the difference between a variable and constant? The difference between variables and constants is that variables can change their value at any time but constants can never change their value.
  • 9. Variables C++ allows declaration of variables anywhere in the scope. For eg: float average;For eg: float average; average = sum/i ; C++ permits initialization of the variables at run time. // Dynamic initialization For eg: float average =sum/i ;
  • 10. Reference Variables C++ defines a reference variable that provide an alias for a previously defined variable. Syntax:Syntax: data- type &reference-name = variable-name It is used to passing arguments to functions For eg: float total = 100; float & sum = total;
  • 11. Reference Variable Major application of reference variables is in passing arguments to functions. void f (int & x) // uses reference {{ x = x + 10; // x is incremented so also m } int main() { int m = 10; f (m); // function call }
  • 12. Operators in C++ C++ has a rich set of operator: Insertion Operator << Extraction Operator >> Scope Resolution Operator :: Pointer-to-member declarator ::*Pointer-to-member declarator ::* Pointer-to-member operator ->* Pointer-to-member operator .* Memory release operator delete Memory allocation operator new Line feed operator endlendl Field width operator setwsetw
  • 13. Scope Resolution Operator Scope resolution operator allows access to global version of a variable. For eg: ::m will always refer to the global m.::m will always refer to the global m. Major application of the scope resolution operator is in classes to identify the class to which a member function belongs. For eg: void employee :: getdata(void) int item :: count
  • 14. Memory Management Operator C++ defines new and delete operators for allocating and freeing the memory . An object is created by using new, and destroyed by using delete as and when required. Syntax: pointer-variable = new data-type; int *p = new int; *p = 25; pointer-variable = new data-type(value); int *p = new int(25); pointer-variable = new data-type[size] int *p = new int [10]; delete pointer-variable; delete p;
  • 15. Manipulators Manipulators are the operators that are used to format the data display. Commonly used manipulators are endl and setw.Commonly used manipulators are endl and setw. endl manipulator causes a line feed to be inserted. setw specifies a field width for printing the value of the variable.
  • 16. Type Cast Operator C++ permits explicit type conversion of variables using type cast operator. average = sum / (float) i ; // C notationaverage = sum / (float) i ; // C notation average = sum / float (i) ; // C ++ notation A type name behaves as if it is a function for converting values to a designated type.
  • 17. this pointer C++ uses a unique keyword this to represent an object that invoke a member function. this is a pointer that points to the object for which thisthis is a pointer that points to the object for which this function was called. For example: A call to a function A.max() will set the pointer this to the address of the object A.
  • 18. Quiz 2 Define Class. A class is a collection of objects of similar type. What are the contents of class? The class consists of data members and member functions. Both of which characterise the object for which the class is defined.
  • 19. Classes and Objects A class is a way to bind the data and its associated functions together. It allows the data to be hidden, if necessary, from external use.use. When defining a class a new ADT is created that can be used like any other built-in type. A class have two parts: Class declaration Class function definitions
  • 20. Class Declaration General form of class declaration: class class_name { private :private : variable declarations; function declarations; public: variable declarations; function declarations; };
  • 21. Class Example class item { int number; float cost;float cost; public: void getdata(int a, float b); void putdata(void); };
  • 22. Quiz 3 What are Access Specifiers? Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.
  • 23. Accessing Class Members We have three basic access types: private public protected Private members can be accessed within the class itself. Public members can be accessed from outside the class. A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.
  • 24. Accessing Class Members The main() cannot contain the statements that access private data members directly. They use the member function to use them. For eg: x.getdata (100, 75.5)x.getdata (100, 75.5) x.number = 100; // illegal Objects communicate by sending and receiving messages. For eg: x.putdata();
  • 25. Defining Member Functions Member functions can be defined in two phases: Outside the class definition Inside the class definition A member function can also be private A private member function can only be called by another function that is a member of its class.
  • 26. Static Data Members A data member of a class can be static. A static member variable has the following characteristics: It is initialized to zero when the first object of its class isIt is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for the entire class. It is visible only within the class Example
  • 27. Static Members Functions A static member function has following characteristics: A static function can have access to only static members (functions or variables) declared in the same class.(functions or variables) declared in the same class. A static member function can be called using the class name as: class-name :: function-name; Eg: item :: showcount(); Example
  • 28. Quiz 4 Define Array. Array is a data structure used to store the elementsArray is a data structure used to store the elements of same type.
  • 29. Array of Objects Similar to structure we can create a array of objects for a class. For eg: employee manager[5]; employee worker[10]; Example
  • 30. Summary ________ defines a scope for the identifiers that are used in the program. Main purpose of defining reference variable is ______. C++ defines ______ and _____ operators for allocating and freeing the memory .and freeing the memory . The main() cannot contain the statements that access private data members directly. (True/False) A private member function can only be called by another function that is a member of its class. (True/False) A static member function can be called using ______ .
  • 31. References Object Oriented Programming with C++ by E. Balagurusamy.Balagurusamy.