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++

More Related Content

What's hot

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 unionstudent
 
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, polymorphismJussi 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
 

What's hot (20)

Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
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
 
structure and union
structure and unionstructure and union
structure and union
 
Basic c#
Basic c#Basic c#
Basic c#
 
Class and object
Class and objectClass and object
Class and object
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
C++ oop
C++ oopC++ oop
C++ oop
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
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++
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Structures
StructuresStructures
Structures
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 

Similar to Introduction to C++

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
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 classtrixiacruz
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
Mufaddal Nullwala
 
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 01Zafor 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
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
InfinityWorld3
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
rsnyder3601
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
shubhra chauhan
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cSunny Shaikh
 

Similar to Introduction to C++ (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
My c++
My c++My c++
My c++
 
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
 
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
 
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...
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
 
02.adt
02.adt02.adt
02.adt
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Bc0037
Bc0037Bc0037
Bc0037
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 

Recently uploaded

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 

Recently uploaded (20)

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 

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.