SlideShare a Scribd company logo
ACA Summer School 2014
Advanced C++
Pankaj Prateek
ACA, CSE, IIT Kanpur
June 24, 2014
Pointers Revisited
Pointers are derived data types that contain memory address
of some other variable
Dereferencing a pointer and pointer arithmetic works exactly in
the same way as with structs and built-in types
Accessing (Class Pointers):
item x(12 ,100);
item* ptr = &x;
//
Ways to access
x.getDetails ();
ptr ->getDetails ();
(*ptr). getDetails ();
// All three ways are equivalent
Pointers Revisited
Pointers are derived data types that contain memory address
of some other variable
Dereferencing a pointer and pointer arithmetic works exactly in
the same way as with structs and built-in types
Accessing (Class Pointers):
item x(12 ,100);
item* ptr = &x;
//
Ways to access
x.getDetails ();
ptr ->getDetails ();
(*ptr). getDetails ();
// All three ways are equivalent
Pointers Revisited
Pointers are derived data types that contain memory address
of some other variable
Dereferencing a pointer and pointer arithmetic works exactly in
the same way as with structs and built-in types
Accessing (Class Pointers):
item x(12 ,100);
item* ptr = &x;
//
Ways to access
x.getDetails ();
ptr ->getDetails ();
(*ptr). getDetails ();
// All three ways are equivalent
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Pointers Revisited
Void Pointers:
Generic pointers which can refer to variables of any type
Need to be typecasted to proper type before dereferencing
Null Pointers:
Pointers to a specific data type which does not point to any
object of that type
“Null” or “0” pointers
Class: this pointer
this pointer is used to represent the object for which the
current member function was called
Automatically passed as an implicit arguement when a
member function is called
*this is the reference to the object which called the function
Class: this pointer
this pointer is used to represent the object for which the
current member function was called
Automatically passed as an implicit arguement when a
member function is called
*this is the reference to the object which called the function
Class: this pointer
this pointer is used to represent the object for which the
current member function was called
Automatically passed as an implicit arguement when a
member function is called
*this is the reference to the object which called the function
Class: this pointer
class Person {
double height;
public:
person& taller(person& x) {
if (x.height > height)
return x;
else
return *this;
}
};
Person A, B, tallest;
tallest = A.taller(B);
Classes without constructors
We have been using member functions to set the values of
member variables
class item {
int number , cost;
public:
void setValue(int itemNum , int itemCost );
void getValue(void );
};
Classes should allow to use customized definitons in the same
way as we use built-in definitions. Initialization and destruction
properties are important for this.
Classes without constructors
We have been using member functions to set the values of
member variables
class item {
int number , cost;
public:
void setValue(int itemNum , int itemCost );
void getValue(void );
};
Classes should allow to use customized definitons in the same
way as we use built-in definitions. Initialization and destruction
properties are important for this.
Class: Constructors
Constructors are special member functions whose task is to
initialize objects of a class.
Constructors have the same name as the class
Constructors are invoked when an object of the class is created.
Constructors do not have a return type
Class: Constructors
Constructors are special member functions whose task is to
initialize objects of a class.
Constructors have the same name as the class
Constructors are invoked when an object of the class is created.
Constructors do not have a return type
Class: Constructors
Constructors are special member functions whose task is to
initialize objects of a class.
Constructors have the same name as the class
Constructors are invoked when an object of the class is created.
Constructors do not have a return type
Class: Constructors
Constructors are special member functions whose task is to
initialize objects of a class.
Constructors have the same name as the class
Constructors are invoked when an object of the class is created.
Constructors do not have a return type
Class: Constructors
class item {
int number;
int cost;
public:
item(void) { // Constructor
number = cost = 0;
}
void getValue(void );
};
int main () {
item soap , pencil , pen;
soap.getValue ();
pencil.getValue ();
pen.getValue ();
}
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Constructor Properties
Constructors should be declared in the public section of the
class, except for some really special cases (singleton design
patterns)
Constructors cannot have a return type (not even void)
Constructors can have multiple parameters and default values
(just like normal functions)
A class can have multiple constructors. All have the same
name (the name of the class), but which constructor is called
depends on the signature of the constructor. This is called
constructor overloading.
Constructors cannot be virtual (discussed later)
Constructors can have reference to an object of the same class
as an input parameter. Such constructors are copy constructors
Class: Default Constructor
Even if when a constructor has not been defined, the compiler
will define a default constructor. This constructor does
nothing, it is an empty constructor.
If some constructors are defined, but they are all non-default,
the compiler will not implicitly define a default constructor.
This might cause problems. Thus, define a default constructor
whenever a non-default constructor is defined.
Class: Default Constructor
Even if when a constructor has not been defined, the compiler
will define a default constructor. This constructor does
nothing, it is an empty constructor.
If some constructors are defined, but they are all non-default,
the compiler will not implicitly define a default constructor.
This might cause problems. Thus, define a default constructor
whenever a non-default constructor is defined.
Class: Default Constructor
class item {
int number , cost;
public:
item (); // default
item(int itemNum , int itemCost) {
number = itemNum; cost = itemCost;
}
};
item pen1 , pen2;
item pencil1 (123, 40), pencil2 (123, 10);
Class: Multiple Constructors
Which constructor is called depends on the constructor
signature.
Class: Copy Constructors
Constructors which are used to declare and initialize an object
from another object, most probably via a reference to that
object.
Class: Copy Constructors
class item {
int number , cost;
public:
item (); // default
item(int itemNum , int itemCost) {
number = itemNum; cost = itemCost;
}
item(item& temp) {
// Accessing private members
number = temp.number;
cost = temp.cost;
}
};
item pen1 (123, 40);
item pen2 (& pen1 );
Private Members
Why can one access private members of another object in the
definition of the copy constructor?
Access modifiers (private, public) work only at class level and
not at object level. Gence two objects of the same class can
access each other’s private members without any error!!
Private Members
Why can one access private members of another object in the
definition of the copy constructor?
Access modifiers (private, public) work only at class level and
not at object level. Gence two objects of the same class can
access each other’s private members without any error!!
Class: Copy Constructors
The process of Initialization of an object through a copy
cnstructor is known as copy initialization.
Class: Destructors
Special functions like constructors. Destroy objects created by
a constructor
They neither have any input arguement, not a return value.
Implicitly called by a compiler when the object goes out of
scope.
Class: Destructors
Special functions like constructors. Destroy objects created by
a constructor
They neither have any input arguement, not a return value.
Implicitly called by a compiler when the object goes out of
scope.
Class: Destructors
Special functions like constructors. Destroy objects created by
a constructor
They neither have any input arguement, not a return value.
Implicitly called by a compiler when the object goes out of
scope.
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Code Reusability
Nobody likes to write code for the same functionality again
and again without any significant improvement
Using already written code is more reliable, saves time, money
and frustration for the programmer of debugging
Example of reuse: for students and teachers, the properties of
the class person are common, and hence should be implented
only once
Reuse of class : Inheritance
Inheritance : Using old classes and their properties to make
new classes
Old class : Base class
New class : Derived class
The derived class inherits, some or all, properties of the base
class
Ad

More Related Content

What's hot (19)

Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
backdoor
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
Mohamad Al_hsan
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
g_hemanth17
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
Raga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
singhadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
Mody Farouk
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
C#ppt
C#pptC#ppt
C#ppt
Sambasivarao Kurakula
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
suraj pandey
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
backdoor
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
Mohamad Al_hsan
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
g_hemanth17
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
Raga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
singhadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
Mody Farouk
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
suraj pandey
 

Similar to Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK (20)

CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
C++
C++C++
C++
vrushali shivsharan
 
C++
C++C++
C++
vrushali shivsharan
 
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyjChapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
berihun18
 
About Python
About PythonAbout Python
About Python
Shao-Chuan Wang
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
saranuru
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
Raghuveer Guthikonda
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
baabtra.com - No. 1 supplier of quality freshers
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
C++ Presen. tation.pptx
C++ Presen.                   tation.pptxC++ Presen.                   tation.pptx
C++ Presen. tation.pptx
mohitsinha7739289047
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Ad

More from Pankaj Prateek (6)

05 graph
05 graph05 graph
05 graph
Pankaj Prateek
 
06 segment trees
06 segment trees06 segment trees
06 segment trees
Pankaj Prateek
 
04 maths
04 maths04 maths
04 maths
Pankaj Prateek
 
03 dp
03 dp03 dp
03 dp
Pankaj Prateek
 
02 greedy, d&c, binary search
02 greedy, d&c, binary search02 greedy, d&c, binary search
02 greedy, d&c, binary search
Pankaj Prateek
 
01 basics and stl
01 basics and stl01 basics and stl
01 basics and stl
Pankaj Prateek
 
Ad

Recently uploaded (20)

Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Routing Riverdale - A New Bus Connection
Routing Riverdale - A New Bus ConnectionRouting Riverdale - A New Bus Connection
Routing Riverdale - A New Bus Connection
jzb7232
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Understanding Structural Loads and Load Paths
Understanding Structural Loads and Load PathsUnderstanding Structural Loads and Load Paths
Understanding Structural Loads and Load Paths
University of Kirkuk
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Novel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth ControlNovel Plug Flow Reactor with Recycle For Growth Control
Novel Plug Flow Reactor with Recycle For Growth Control
Chris Harding
 
Routing Riverdale - A New Bus Connection
Routing Riverdale - A New Bus ConnectionRouting Riverdale - A New Bus Connection
Routing Riverdale - A New Bus Connection
jzb7232
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
Redirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to RickrollsRedirects Unraveled: From Lost Links to Rickrolls
Redirects Unraveled: From Lost Links to Rickrolls
Kritika Garg
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Building-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdfBuilding-Services-Introduction-Notes.pdf
Building-Services-Introduction-Notes.pdf
Lawrence Omai
 

Advanced CPP Lecture 2- Summer School 2014 - ACA CSE IITK

  • 1. ACA Summer School 2014 Advanced C++ Pankaj Prateek ACA, CSE, IIT Kanpur June 24, 2014
  • 2. Pointers Revisited Pointers are derived data types that contain memory address of some other variable Dereferencing a pointer and pointer arithmetic works exactly in the same way as with structs and built-in types Accessing (Class Pointers): item x(12 ,100); item* ptr = &x; // Ways to access x.getDetails (); ptr ->getDetails (); (*ptr). getDetails (); // All three ways are equivalent
  • 3. Pointers Revisited Pointers are derived data types that contain memory address of some other variable Dereferencing a pointer and pointer arithmetic works exactly in the same way as with structs and built-in types Accessing (Class Pointers): item x(12 ,100); item* ptr = &x; // Ways to access x.getDetails (); ptr ->getDetails (); (*ptr). getDetails (); // All three ways are equivalent
  • 4. Pointers Revisited Pointers are derived data types that contain memory address of some other variable Dereferencing a pointer and pointer arithmetic works exactly in the same way as with structs and built-in types Accessing (Class Pointers): item x(12 ,100); item* ptr = &x; // Ways to access x.getDetails (); ptr ->getDetails (); (*ptr). getDetails (); // All three ways are equivalent
  • 5. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 6. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 7. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 8. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 9. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 10. Pointers Revisited Void Pointers: Generic pointers which can refer to variables of any type Need to be typecasted to proper type before dereferencing Null Pointers: Pointers to a specific data type which does not point to any object of that type “Null” or “0” pointers
  • 11. Class: this pointer this pointer is used to represent the object for which the current member function was called Automatically passed as an implicit arguement when a member function is called *this is the reference to the object which called the function
  • 12. Class: this pointer this pointer is used to represent the object for which the current member function was called Automatically passed as an implicit arguement when a member function is called *this is the reference to the object which called the function
  • 13. Class: this pointer this pointer is used to represent the object for which the current member function was called Automatically passed as an implicit arguement when a member function is called *this is the reference to the object which called the function
  • 14. Class: this pointer class Person { double height; public: person& taller(person& x) { if (x.height > height) return x; else return *this; } }; Person A, B, tallest; tallest = A.taller(B);
  • 15. Classes without constructors We have been using member functions to set the values of member variables class item { int number , cost; public: void setValue(int itemNum , int itemCost ); void getValue(void ); }; Classes should allow to use customized definitons in the same way as we use built-in definitions. Initialization and destruction properties are important for this.
  • 16. Classes without constructors We have been using member functions to set the values of member variables class item { int number , cost; public: void setValue(int itemNum , int itemCost ); void getValue(void ); }; Classes should allow to use customized definitons in the same way as we use built-in definitions. Initialization and destruction properties are important for this.
  • 17. Class: Constructors Constructors are special member functions whose task is to initialize objects of a class. Constructors have the same name as the class Constructors are invoked when an object of the class is created. Constructors do not have a return type
  • 18. Class: Constructors Constructors are special member functions whose task is to initialize objects of a class. Constructors have the same name as the class Constructors are invoked when an object of the class is created. Constructors do not have a return type
  • 19. Class: Constructors Constructors are special member functions whose task is to initialize objects of a class. Constructors have the same name as the class Constructors are invoked when an object of the class is created. Constructors do not have a return type
  • 20. Class: Constructors Constructors are special member functions whose task is to initialize objects of a class. Constructors have the same name as the class Constructors are invoked when an object of the class is created. Constructors do not have a return type
  • 21. Class: Constructors class item { int number; int cost; public: item(void) { // Constructor number = cost = 0; } void getValue(void ); }; int main () { item soap , pencil , pen; soap.getValue (); pencil.getValue (); pen.getValue (); }
  • 22. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 23. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 24. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 25. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 26. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 27. Class: Constructor Properties Constructors should be declared in the public section of the class, except for some really special cases (singleton design patterns) Constructors cannot have a return type (not even void) Constructors can have multiple parameters and default values (just like normal functions) A class can have multiple constructors. All have the same name (the name of the class), but which constructor is called depends on the signature of the constructor. This is called constructor overloading. Constructors cannot be virtual (discussed later) Constructors can have reference to an object of the same class as an input parameter. Such constructors are copy constructors
  • 28. Class: Default Constructor Even if when a constructor has not been defined, the compiler will define a default constructor. This constructor does nothing, it is an empty constructor. If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. This might cause problems. Thus, define a default constructor whenever a non-default constructor is defined.
  • 29. Class: Default Constructor Even if when a constructor has not been defined, the compiler will define a default constructor. This constructor does nothing, it is an empty constructor. If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. This might cause problems. Thus, define a default constructor whenever a non-default constructor is defined.
  • 30. Class: Default Constructor class item { int number , cost; public: item (); // default item(int itemNum , int itemCost) { number = itemNum; cost = itemCost; } }; item pen1 , pen2; item pencil1 (123, 40), pencil2 (123, 10);
  • 31. Class: Multiple Constructors Which constructor is called depends on the constructor signature.
  • 32. Class: Copy Constructors Constructors which are used to declare and initialize an object from another object, most probably via a reference to that object.
  • 33. Class: Copy Constructors class item { int number , cost; public: item (); // default item(int itemNum , int itemCost) { number = itemNum; cost = itemCost; } item(item& temp) { // Accessing private members number = temp.number; cost = temp.cost; } }; item pen1 (123, 40); item pen2 (& pen1 );
  • 34. Private Members Why can one access private members of another object in the definition of the copy constructor? Access modifiers (private, public) work only at class level and not at object level. Gence two objects of the same class can access each other’s private members without any error!!
  • 35. Private Members Why can one access private members of another object in the definition of the copy constructor? Access modifiers (private, public) work only at class level and not at object level. Gence two objects of the same class can access each other’s private members without any error!!
  • 36. Class: Copy Constructors The process of Initialization of an object through a copy cnstructor is known as copy initialization.
  • 37. Class: Destructors Special functions like constructors. Destroy objects created by a constructor They neither have any input arguement, not a return value. Implicitly called by a compiler when the object goes out of scope.
  • 38. Class: Destructors Special functions like constructors. Destroy objects created by a constructor They neither have any input arguement, not a return value. Implicitly called by a compiler when the object goes out of scope.
  • 39. Class: Destructors Special functions like constructors. Destroy objects created by a constructor They neither have any input arguement, not a return value. Implicitly called by a compiler when the object goes out of scope.
  • 40. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 41. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 42. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 43. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 44. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 45. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 46. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  • 47. Code Reusability Nobody likes to write code for the same functionality again and again without any significant improvement Using already written code is more reliable, saves time, money and frustration for the programmer of debugging Example of reuse: for students and teachers, the properties of the class person are common, and hence should be implented only once Reuse of class : Inheritance Inheritance : Using old classes and their properties to make new classes Old class : Base class New class : Derived class The derived class inherits, some or all, properties of the base class
  翻译: