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

More Related Content

PDF
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
PPTX
JavaScript OOPS Implimentation
DOCX
Introduction to object oriented programming concepts
PPT
Defining classes-and-objects-1.0
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
Classes and objects1
PPT
Java: Objects and Object References
PPT
Unidad o informatica en ingles
Advanced CPP Lecture 1- Summer School 2014 - ACA CSE IITK
JavaScript OOPS Implimentation
Introduction to object oriented programming concepts
Defining classes-and-objects-1.0
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Classes and objects1
Java: Objects and Object References
Unidad o informatica en ingles

What's hot (19)

PPTX
Object Oriended Programming with Java
PPT
Object and Classes in Java
PDF
Lect 1-java object-classes
PPTX
Class introduction in java
PPT
Class and object in C++
PPT
object oriented programming language by c++
PPTX
Classes and objects
PPTX
Constructors and destructors
PDF
Class and object in C++ By Pawan Thakur
PPTX
[OOP - Lec 20,21] Inheritance
PPT
Introduction To Csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
PPS
Introduction to CSharp
PPS
Introduction to class in java
PDF
How to write you first class in c++ object oriented programming
PPT
Classes & objects new
PPT
vb.net Constructor and destructor
Object Oriended Programming with Java
Object and Classes in Java
Lect 1-java object-classes
Class introduction in java
Class and object in C++
object oriented programming language by c++
Classes and objects
Constructors and destructors
Class and object in C++ By Pawan Thakur
[OOP - Lec 20,21] Inheritance
Introduction To Csharp
Introduction to csharp
Introduction to csharp
Introduction to CSharp
Introduction to class in java
How to write you first class in c++ object oriented programming
Classes & objects new
vb.net Constructor and destructor

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

PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
PPTX
About Python
PPT
Advanced c#
PPTX
Class and object
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
PPTX
java module 2java module 2java module 2java module 2
PPTX
Objects and Types C#
PPT
4. OBJECT ORIENTED PROGRAMMING WITH SCOPE AND FEATURES
PDF
PPTX
Constructor and destructor
PPTX
C# classes objects
PPTX
Lecture 5
PPTX
C++ Presen. tation.pptx
PPTX
Ch-2ppt.pptx
PPT
ConsTRUCTION AND DESTRUCTION
PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Chapter4.pptxdgdhgfshsfhtgjsjryjusryjryjursyj
About Python
Advanced c#
Class and object
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
java module 2java module 2java module 2java module 2
Objects and Types C#
4. OBJECT ORIENTED PROGRAMMING WITH SCOPE AND FEATURES
Constructor and destructor
C# classes objects
Lecture 5
C++ Presen. tation.pptx
Ch-2ppt.pptx
ConsTRUCTION AND DESTRUCTION
C++ Programming Course
Ccourse 140618093931-phpapp02

More from Pankaj Prateek (6)

PDF
05 graph
PDF
06 segment trees
PDF
04 maths
PDF
PDF
02 greedy, d&c, binary search
PDF
01 basics and stl
05 graph
06 segment trees
04 maths
02 greedy, d&c, binary search
01 basics and stl

Recently uploaded (20)

PDF
Project_Mgmt_Institute_- Marc Marc Marc.pdf
PDF
Application of smart robotics in the supply chain
PDF
ANTIOXIDANT AND ANTIMICROBIAL ACTIVITIES OF ALGERIAN POPULUS NIGRA L. BUDS EX...
PPTX
Cloud Security and Privacy-Module-1.pptx
PPTX
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
PPTX
non conventional energy resorses material unit-1
PDF
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
PDF
1.-fincantieri-investor-presentation2.pdf
PPTX
Research Writing, Mechanical Engineering
PPTX
Unit I - Mechatronics.pptx presentation
PDF
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
ITEC 1010 - Networks and Cloud Computing
PDF
IAE-V2500 Engine Airbus Family A319/320
PPTX
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
PDF
PhD defense presentation in field of Computer Science
PDF
Electrical & Computer Engineering: An International Journal (ECIJ)
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PPTX
Module 1 – Introduction to Computer Networks: Foundations of Data Communicati...
PPTX
Software-Development-Life-Cycle-SDLC.pptx
Project_Mgmt_Institute_- Marc Marc Marc.pdf
Application of smart robotics in the supply chain
ANTIOXIDANT AND ANTIMICROBIAL ACTIVITIES OF ALGERIAN POPULUS NIGRA L. BUDS EX...
Cloud Security and Privacy-Module-1.pptx
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
non conventional energy resorses material unit-1
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
1.-fincantieri-investor-presentation2.pdf
Research Writing, Mechanical Engineering
Unit I - Mechatronics.pptx presentation
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
ITEC 1010 - Networks and Cloud Computing
IAE-V2500 Engine Airbus Family A319/320
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
PhD defense presentation in field of Computer Science
Electrical & Computer Engineering: An International Journal (ECIJ)
Software defined netwoks is useful to learn NFV and virtual Lans
Module 1 – Introduction to Computer Networks: Foundations of Data Communicati...
Software-Development-Life-Cycle-SDLC.pptx

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