The document provides an overview of JavaScript design patterns including creational, structural, and behavioral patterns. It discusses common patterns like the module pattern, prototype pattern, factory pattern, decorator pattern, observer pattern, and more. Code examples are provided to demonstrate how each pattern works in JavaScript.
The document discusses polymorphism in C++. It shows how declaring functions as virtual in a base class and derived classes allows dynamic binding via pointers to the base class. When calling virtual functions through a base class pointer, the function called is determined by the actual object type, not the pointer type, allowing base class pointers to transparently access overridden functions in derived classes. This allows generic treatment of objects through their common base class.
The document discusses various object-oriented programming concepts in C++ like classes, objects, member functions, data members, constructors, destructors, friend functions, and namespaces. It provides examples of defining classes with data members and member functions, declaring objects of a class, and using constructors and destructors. It also explains concepts like overloaded constructors, copy constructors, nested classes, dynamic initialization of objects, and friend functions.
This document discusses various concepts related to classes and objects in C++, including member functions, data members, constructors, destructors, friend functions, and nested classes. It provides examples of defining member functions inside and outside the class, different access specifiers for data members, examples of friend functions and classes, returning objects from functions, arrays of objects, and nested classes. It also discusses constructors in more detail, including overloaded constructors, copy constructors, dynamic initialization of objects, constructors for primitive types, and constructors with default arguments.
Virtual functions allow functions in derived classes to override functions in base classes. When a base class pointer points to a derived class object, calling a virtual function through the pointer will call the derived class's version. This allows the same interface to behave differently based on the actual object. Virtual functions are useful for mathematical operations, where a base class defines an interface and derived classes perform specific calculations like addition, subtraction etc. depending on the object type pointed to by the base class pointer.
The document discusses object oriented programming concepts in C++ like classes, objects, data members, member functions etc. It provides code examples to demonstrate defining classes with data members and member functions, creating objects of a class, accessing data members and calling member functions. It also shows the difference between declaring members as public or private. One example creates an Employee class with functions to get employee details like salary, working hours and calculate final salary. Another example creates an Employee class to store and print details of 3 employees like name, year of joining and address.
The document provides an overview of JavaScript design patterns including creational, structural, and behavioral patterns. It discusses common patterns like the module pattern, prototype pattern, factory pattern, decorator pattern, observer pattern, and more. Code examples are provided to demonstrate how each pattern works in JavaScript.
The document discusses polymorphism in C++. It shows how declaring functions as virtual in a base class and derived classes allows dynamic binding via pointers to the base class. When calling virtual functions through a base class pointer, the function called is determined by the actual object type, not the pointer type, allowing base class pointers to transparently access overridden functions in derived classes. This allows generic treatment of objects through their common base class.
The document discusses various object-oriented programming concepts in C++ like classes, objects, member functions, data members, constructors, destructors, friend functions, and namespaces. It provides examples of defining classes with data members and member functions, declaring objects of a class, and using constructors and destructors. It also explains concepts like overloaded constructors, copy constructors, nested classes, dynamic initialization of objects, and friend functions.
This document discusses various concepts related to classes and objects in C++, including member functions, data members, constructors, destructors, friend functions, and nested classes. It provides examples of defining member functions inside and outside the class, different access specifiers for data members, examples of friend functions and classes, returning objects from functions, arrays of objects, and nested classes. It also discusses constructors in more detail, including overloaded constructors, copy constructors, dynamic initialization of objects, constructors for primitive types, and constructors with default arguments.
Virtual functions allow functions in derived classes to override functions in base classes. When a base class pointer points to a derived class object, calling a virtual function through the pointer will call the derived class's version. This allows the same interface to behave differently based on the actual object. Virtual functions are useful for mathematical operations, where a base class defines an interface and derived classes perform specific calculations like addition, subtraction etc. depending on the object type pointed to by the base class pointer.
The document discusses object oriented programming concepts in C++ like classes, objects, data members, member functions etc. It provides code examples to demonstrate defining classes with data members and member functions, creating objects of a class, accessing data members and calling member functions. It also shows the difference between declaring members as public or private. One example creates an Employee class with functions to get employee details like salary, working hours and calculate final salary. Another example creates an Employee class to store and print details of 3 employees like name, year of joining and address.
The document discusses object-oriented programming concepts in C++ including classes, objects, data members, member functions, access specifiers, and more. It provides examples to demonstrate defining a class with data members and member functions, declaring objects of a class, accessing class members using dot operator, passing objects to functions by value and reference, returning objects from functions, static class members, and nested classes. It also addresses scope of classes and objects, inline functions, and constant member functions.
The document introduces several uncommon design patterns:
1) Null Object pattern avoids using null values by implementing a "null" object that respects the expected interface.
2) Encapsulated Context pattern encapsulates common services/data needed by different components in a single context object rather than using global objects.
3) Role Object pattern handles modeling problems where an element can have different roles by using role objects that are added to and removed from a core object.
4) ISP & Object Adaptation pattern allows objects to adapt to different protocols by checking for direct implementation, using registered adapters, or throwing exceptions.
5) Essence pattern uses an "essence" class to encapsulate an object's configuration and
The document discusses object-oriented programming concepts like classes, objects, member functions, data members, constructors, and encapsulation. It explains that a class defines the structure and behavior of objects, with data members representing attributes and member functions representing behaviors. Constructors initialize an object's data when it is created. Encapsulation protects data by making it private and only accessible through public member functions.
- Constructors are special member functions that are used to initialize objects of a class. They are automatically called when an object is created.
- There are different types of constructors including default, parameterized, and copy constructors. Default constructors take no parameters while parameterized constructors allow passing initial values.
- Constructors can be explicitly or implicitly called. Implicit calls are made when an object is declared while explicit calls directly call the constructor. Constructors ensure objects are properly initialized.
In this short presentation struct inheritance and virtual member function in pure C are demonstrated. With these fundamental techniques, more advanced design patterns are unlocked.
The document provides information about C++ aptitude and object-oriented programming (OOP) concepts. It contains sample C++ code snippets and questions with explanations to test understanding of topics like classes, inheritance, polymorphism, operator overloading, templates, and more.
The document provides an outline of a lecture on object-oriented JavaScript and inheritance. It covers key concepts like objects, properties, methods, object literals, constructor functions, and prototype inheritance. It discusses creating and accessing objects, adding and removing properties, passing objects by reference, comparing objects, and the prototype chain. Private and privileged methods and properties are explained. Different approaches to implementing inheritance like prototype chaining are also summarized.
The document discusses object oriented programming concepts like class, object, encapsulation, inheritance and polymorphism. It provides examples of defining a class with data members and member functions in C++. It also explains constructors, destructors, different types of constructors like default, parameterized and copy constructor. Examples are given to illustrate how objects are created from a class and how member functions can be defined internally or externally.
C/C++ Programming interview questions and answers document discusses key concepts in C++ including encapsulation, inheritance, polymorphism, constructors, destructors, copy constructors, references, virtual functions, abstract classes, and memory alignment. The document provides definitions and examples to explain each concept.
This document provides an overview of object-oriented programming concepts in Java, including classes, objects, variables, methods, constructors, abstraction, encapsulation, inheritance, and polymorphism. It defines classes and objects, and describes how classes act as blueprints for objects. It explains the syntax for defining classes and class members like variables and methods. It also covers method overloading, different types of methods, and how constructors are used to initialize objects. The document concludes with brief explanations of abstraction, encapsulation, inheritance, and polymorphism as fundamental principles of object-oriented design.
I am Irene M. I am a C++ Homework Expert at cpphomeworkhelp.com. I hold a Masters in Programming from from California, USA. I have been helping students with their homework for the past 10 years. I solve homework related to C++.
Visit cpphomeworkhelp.com or email info@cpphomeworkhelp.com. You can also call on +1 678 648 4277 for any assistance with C++ Homework.
This document provides an overview of object-oriented programming concepts in C++, including objects, classes, data abstraction, encapsulation, inheritance, and polymorphism. It defines each concept, provides examples in C++ code, and explains how they are implemented and relate to each other. The document is presented as part of a mentoring program to teach OOP concepts.
The document discusses objects and classes in C++. It covers creating objects using new, constructors, destructors, accessor and mutator methods, default copy constructors, returning objects from functions, and variable scope. Specifically, it provides examples of creating Circle objects with constructors, defining destructors to deallocate memory, using getter and setter methods to access private data, and copying objects using the default copy constructor.
The document discusses different types of memory areas in C++ including stack, heap, static, and const data areas. It compares pointers and references, explaining that pointers can be null while references must always refer to a valid object. The document also covers memory management topics like new and delete operators, placement new, and smart pointers. Common memory problems are outlined along with solutions like using destructors and smart pointers to avoid leaks.
C# is similar to C++ but easier to use, as it does not support pointers, multiple inheritance, header files or global variables. Everything must live within a class or struct. The basic syntax will be familiar to C++ programmers. Key features include properties, interfaces, foreach loops, and delegates for event handling. Properties allow custom getter and setter logic and are preferred over public fields. Delegates provide a type-safe way to link methods, and events build on this to prevent issues with multicast delegates. Generics and assemblies are analogous to C++ templates and deployment units.
The document discusses Scala concepts including higher-order functions, anonymous functions, currying, tail recursion, classes and objects, properties with getters and setters, object-private fields, auxiliary constructors, primary constructors, nested classes, and singleton objects. It provides code examples to illustrate these Scala features and explains how they work and how they compare to similar concepts in other languages like Java. The document is intended as a lesson on these Scala programming concepts.
The document discusses object-oriented programming concepts in C++ including classes, objects, data members, member functions, access specifiers, and more. It provides examples to demonstrate defining a class with data members and member functions, declaring objects of a class, accessing class members using dot operator, passing objects to functions by value and reference, returning objects from functions, static class members, and nested classes. It also addresses scope of classes and objects, inline functions, and constant member functions.
The document introduces several uncommon design patterns:
1) Null Object pattern avoids using null values by implementing a "null" object that respects the expected interface.
2) Encapsulated Context pattern encapsulates common services/data needed by different components in a single context object rather than using global objects.
3) Role Object pattern handles modeling problems where an element can have different roles by using role objects that are added to and removed from a core object.
4) ISP & Object Adaptation pattern allows objects to adapt to different protocols by checking for direct implementation, using registered adapters, or throwing exceptions.
5) Essence pattern uses an "essence" class to encapsulate an object's configuration and
The document discusses object-oriented programming concepts like classes, objects, member functions, data members, constructors, and encapsulation. It explains that a class defines the structure and behavior of objects, with data members representing attributes and member functions representing behaviors. Constructors initialize an object's data when it is created. Encapsulation protects data by making it private and only accessible through public member functions.
- Constructors are special member functions that are used to initialize objects of a class. They are automatically called when an object is created.
- There are different types of constructors including default, parameterized, and copy constructors. Default constructors take no parameters while parameterized constructors allow passing initial values.
- Constructors can be explicitly or implicitly called. Implicit calls are made when an object is declared while explicit calls directly call the constructor. Constructors ensure objects are properly initialized.
In this short presentation struct inheritance and virtual member function in pure C are demonstrated. With these fundamental techniques, more advanced design patterns are unlocked.
The document provides information about C++ aptitude and object-oriented programming (OOP) concepts. It contains sample C++ code snippets and questions with explanations to test understanding of topics like classes, inheritance, polymorphism, operator overloading, templates, and more.
The document provides an outline of a lecture on object-oriented JavaScript and inheritance. It covers key concepts like objects, properties, methods, object literals, constructor functions, and prototype inheritance. It discusses creating and accessing objects, adding and removing properties, passing objects by reference, comparing objects, and the prototype chain. Private and privileged methods and properties are explained. Different approaches to implementing inheritance like prototype chaining are also summarized.
The document discusses object oriented programming concepts like class, object, encapsulation, inheritance and polymorphism. It provides examples of defining a class with data members and member functions in C++. It also explains constructors, destructors, different types of constructors like default, parameterized and copy constructor. Examples are given to illustrate how objects are created from a class and how member functions can be defined internally or externally.
C/C++ Programming interview questions and answers document discusses key concepts in C++ including encapsulation, inheritance, polymorphism, constructors, destructors, copy constructors, references, virtual functions, abstract classes, and memory alignment. The document provides definitions and examples to explain each concept.
This document provides an overview of object-oriented programming concepts in Java, including classes, objects, variables, methods, constructors, abstraction, encapsulation, inheritance, and polymorphism. It defines classes and objects, and describes how classes act as blueprints for objects. It explains the syntax for defining classes and class members like variables and methods. It also covers method overloading, different types of methods, and how constructors are used to initialize objects. The document concludes with brief explanations of abstraction, encapsulation, inheritance, and polymorphism as fundamental principles of object-oriented design.
I am Irene M. I am a C++ Homework Expert at cpphomeworkhelp.com. I hold a Masters in Programming from from California, USA. I have been helping students with their homework for the past 10 years. I solve homework related to C++.
Visit cpphomeworkhelp.com or email info@cpphomeworkhelp.com. You can also call on +1 678 648 4277 for any assistance with C++ Homework.
This document provides an overview of object-oriented programming concepts in C++, including objects, classes, data abstraction, encapsulation, inheritance, and polymorphism. It defines each concept, provides examples in C++ code, and explains how they are implemented and relate to each other. The document is presented as part of a mentoring program to teach OOP concepts.
The document discusses objects and classes in C++. It covers creating objects using new, constructors, destructors, accessor and mutator methods, default copy constructors, returning objects from functions, and variable scope. Specifically, it provides examples of creating Circle objects with constructors, defining destructors to deallocate memory, using getter and setter methods to access private data, and copying objects using the default copy constructor.
The document discusses different types of memory areas in C++ including stack, heap, static, and const data areas. It compares pointers and references, explaining that pointers can be null while references must always refer to a valid object. The document also covers memory management topics like new and delete operators, placement new, and smart pointers. Common memory problems are outlined along with solutions like using destructors and smart pointers to avoid leaks.
C# is similar to C++ but easier to use, as it does not support pointers, multiple inheritance, header files or global variables. Everything must live within a class or struct. The basic syntax will be familiar to C++ programmers. Key features include properties, interfaces, foreach loops, and delegates for event handling. Properties allow custom getter and setter logic and are preferred over public fields. Delegates provide a type-safe way to link methods, and events build on this to prevent issues with multicast delegates. Generics and assemblies are analogous to C++ templates and deployment units.
The document discusses Scala concepts including higher-order functions, anonymous functions, currying, tail recursion, classes and objects, properties with getters and setters, object-private fields, auxiliary constructors, primary constructors, nested classes, and singleton objects. It provides code examples to illustrate these Scala features and explains how they work and how they compare to similar concepts in other languages like Java. The document is intended as a lesson on these Scala programming concepts.
Similar to Object Oriented Programming using C++: Ch11 Virtual Functions.pptx (20)
International Conference on NLP, Artificial Intelligence, Machine Learning an...gerogepatton
International Conference on NLP, Artificial Intelligence, Machine Learning and Applications (NLAIM 2024) offers a premier global platform for exchanging insights and findings in the theory, methodology, and applications of NLP, Artificial Intelligence, Machine Learning, and their applications. The conference seeks substantial contributions across all key domains of NLP, Artificial Intelligence, Machine Learning, and their practical applications, aiming to foster both theoretical advancements and real-world implementations. With a focus on facilitating collaboration between researchers and practitioners from academia and industry, the conference serves as a nexus for sharing the latest developments in the field.
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...IJECEIAES
Medical image analysis has witnessed significant advancements with deep learning techniques. In the domain of brain tumor segmentation, the ability to
precisely delineate tumor boundaries from magnetic resonance imaging (MRI)
scans holds profound implications for diagnosis. This study presents an ensemble convolutional neural network (CNN) with transfer learning, integrating
the state-of-the-art Deeplabv3+ architecture with the ResNet18 backbone. The
model is rigorously trained and evaluated, exhibiting remarkable performance
metrics, including an impressive global accuracy of 99.286%, a high-class accuracy of 82.191%, a mean intersection over union (IoU) of 79.900%, a weighted
IoU of 98.620%, and a Boundary F1 (BF) score of 83.303%. Notably, a detailed comparative analysis with existing methods showcases the superiority of
our proposed model. These findings underscore the model’s competence in precise brain tumor localization, underscoring its potential to revolutionize medical
image analysis and enhance healthcare outcomes. This research paves the way
for future exploration and optimization of advanced CNN models in medical
imaging, emphasizing addressing false positives and resource efficiency.
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTjpsjournal1
The rivalry between prominent international actors for dominance over Central Asia's hydrocarbon
reserves and the ancient silk trade route, along with China's diplomatic endeavours in the area, has been
referred to as the "New Great Game." This research centres on the power struggle, considering
geopolitical, geostrategic, and geoeconomic variables. Topics including trade, political hegemony, oil
politics, and conventional and nontraditional security are all explored and explained by the researcher.
Using Mackinder's Heartland, Spykman Rimland, and Hegemonic Stability theories, examines China's role
in Central Asia. This study adheres to the empirical epistemological method and has taken care of
objectivity. This study analyze primary and secondary research documents critically to elaborate role of
china’s geo economic outreach in central Asian countries and its future prospect. China is thriving in trade,
pipeline politics, and winning states, according to this study, thanks to important instruments like the
Shanghai Cooperation Organisation and the Belt and Road Economic Initiative. According to this study,
China is seeing significant success in commerce, pipeline politics, and gaining influence on other
governments. This success may be attributed to the effective utilisation of key tools such as the Shanghai
Cooperation Organisation and the Belt and Road Economic Initiative.
Comparative analysis between traditional aquaponics and reconstructed aquapon...bijceesjournal
The aquaponic system of planting is a method that does not require soil usage. It is a method that only needs water, fish, lava rocks (a substitute for soil), and plants. Aquaponic systems are sustainable and environmentally friendly. Its use not only helps to plant in small spaces but also helps reduce artificial chemical use and minimizes excess water use, as aquaponics consumes 90% less water than soil-based gardening. The study applied a descriptive and experimental design to assess and compare conventional and reconstructed aquaponic methods for reproducing tomatoes. The researchers created an observation checklist to determine the significant factors of the study. The study aims to determine the significant difference between traditional aquaponics and reconstructed aquaponics systems propagating tomatoes in terms of height, weight, girth, and number of fruits. The reconstructed aquaponics system’s higher growth yield results in a much more nourished crop than the traditional aquaponics system. It is superior in its number of fruits, height, weight, and girth measurement. Moreover, the reconstructed aquaponics system is proven to eliminate all the hindrances present in the traditional aquaponics system, which are overcrowding of fish, algae growth, pest problems, contaminated water, and dead fish.
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...IJECEIAES
Climate change's impact on the planet forced the United Nations and governments to promote green energies and electric transportation. The deployments of photovoltaic (PV) and electric vehicle (EV) systems gained stronger momentum due to their numerous advantages over fossil fuel types. The advantages go beyond sustainability to reach financial support and stability. The work in this paper introduces the hybrid system between PV and EV to support industrial and commercial plants. This paper covers the theoretical framework of the proposed hybrid system including the required equation to complete the cost analysis when PV and EV are present. In addition, the proposed design diagram which sets the priorities and requirements of the system is presented. The proposed approach allows setup to advance their power stability, especially during power outages. The presented information supports researchers and plant owners to complete the necessary analysis while promoting the deployment of clean energy. The result of a case study that represents a dairy milk farmer supports the theoretical works and highlights its advanced benefits to existing plants. The short return on investment of the proposed approach supports the paper's novelty approach for the sustainable electrical system. In addition, the proposed system allows for an isolated power setup without the need for a transmission line which enhances the safety of the electrical network
Embedded machine learning-based road conditions and driving behavior monitoringIJECEIAES
Car accident rates have increased in recent years, resulting in losses in human lives, properties, and other financial costs. An embedded machine learning-based system is developed to address this critical issue. The system can monitor road conditions, detect driving patterns, and identify aggressive driving behaviors. The system is based on neural networks trained on a comprehensive dataset of driving events, driving styles, and road conditions. The system effectively detects potential risks and helps mitigate the frequency and impact of accidents. The primary goal is to ensure the safety of drivers and vehicles. Collecting data involved gathering information on three key road events: normal street and normal drive, speed bumps, circular yellow speed bumps, and three aggressive driving actions: sudden start, sudden stop, and sudden entry. The gathered data is processed and analyzed using a machine learning system designed for limited power and memory devices. The developed system resulted in 91.9% accuracy, 93.6% precision, and 92% recall. The achieved inference time on an Arduino Nano 33 BLE Sense with a 32-bit CPU running at 64 MHz is 34 ms and requires 2.6 kB peak RAM and 139.9 kB program flash memory, making it suitable for resource-constrained embedded systems.
Batteries -Introduction – Types of Batteries – discharging and charging of battery - characteristics of battery –battery rating- various tests on battery- – Primary battery: silver button cell- Secondary battery :Ni-Cd battery-modern battery: lithium ion battery-maintenance of batteries-choices of batteries for electric vehicle applications.
Fuel Cells: Introduction- importance and classification of fuel cells - description, principle, components, applications of fuel cells: H2-O2 fuel cell, alkaline fuel cell, molten carbonate fuel cell and direct methanol fuel cells.
2. Virtual Functions
High Level Languages
Easy to develop and update
Virtual means existing in appearance but not in reality.
When virtual functions are used, a program that
appears to be calling a function of one class may in
reality be calling a function of a different class.
Why are virtual functions needed?
Suppose you have a number of objects of different classes but you want to
put them all in an array and perform a particular operation on them using
the same function call.
For example, suppose a graphics program includes several different shapes: a
triangle, a ball, a square, and so on. Each of these classes has a member
function draw() that causes the object to be drawn on the screen.
3. Virtual Functions
Now suppose you plan to make a picture by grouping a number of these
elements together, and you want to draw the picture in a convenient way.
One approach is to create an array that holds pointers to all the different
objects in the picture.
Shape Pointers Array
Index Number [0] [1] [2] [3] [4]
Shape
Pointer
Square
Object
Rectangle
Object
Triangle
Object
Circle
Object
Triangle
Object
4. Virtual Functions
The array might be defined like this:
shape* ptrarr[100]; // array of 100 pointers to shapes
If you insert pointers to all the shapes into this array, you can then draw an
entire picture using a simple loop:
for(int j=0 ; j<N ; j++) {
ptrarr[j]->draw(); // (*ptrarr[j] ).draw()
}
This is an amazing capability: Completely different functions are executed by
the same function call.
If the pointer in ptrarr points to a ball object, the function that draws a ball is
called; if it points to a triangle object, the triangle-drawing function is called.
5. Virtual Functions
This is called polymorphism, which means different forms. The functions have
the same appearance, the draw() expression, but different actual functions are
called, depending on the contents of ptrarr[j].
Polymorphism is one of the key features of object-oriented programming,
after classes and inheritance.
For the polymorphic approach to work, several conditions must be met.
First, all the different classes of shapes, such as balls and triangles, must be
descended from a single base class (called shape).
Second, the draw() function must be declared to be virtual in the base class.
6. // Normal functions accessed from pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Parent { // Parent class
public:
void Show() {
cout << "I am Parent n";
}
};
// Derived class Child1
class Child1 : public Parent {
public:
void Show() {
cout << "I am Child 1 n";
}
};
// Derived class Child2
class Child2 : public Parent {
6
Normal Member Functions Accessed with Pointer
public:
void Show() {
cout << "I am Child 2 n";
}
};
int main() {
Child1 C1, C2;
Parent* P; // pointer to Parent class
P = &C1; // put the address of object C1 in
// pointer P. The rule is that pointer to the
// object of a Child class is type compatible
// with the pointer to the objects of the
// Parent class.
P->Show(); // execute P->Show() OR (*P).Show();
// the function in Parent Class is executed.
P = &C2; // put address of object C1 in P.
P->Show(); // execute P->Show()
// the function in Parent Class is executed.
system("PAUSE"); return 0;
}
1 2
7. 7
Normal Member Functions Accessed with Pointer
&Child1 Parent
Show()
Child1
Show()
Child2
Show()
ptr
Ptr->Show()
&Child2
ptr
Ptr->Show()
Figure: Non Virtual Pointer Access
8. // Normal functions accessed from pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Parent { // Parent class
public:
virtual void Show() {
cout << "I am Parent n";
}
};
// Derived class Child1
class Child1 : public Parent {
public:
void Show() {
cout << "I am Child 1 n";
}
};
// Derived class Child2
class Child2 : public Parent {
8
Virtual Member Functions Accessed with Pointer
public:
void Show() { cout << "I am Child 2 n"; }
};
int main() {
Child1 C1,
Child2 C2;
Parent* P; // pointer to Parent class
P = &C1; // put the address of object C1 in
// pointer P. The rule is that pointer to the
// object of a Child class is type compatible
// with the pointer to the objects of the
// Parent class.
P->Show(); // execute P->Show() OR (*P).Show();
// the function in Child1 Class is executed.
P = &C2; // put address of object C1 in P.
P->Show(); // execute P->Show()
// the function in Child2 Class is executed.
system("PAUSE"); return 0;
}
1 2
10. Late Binding
Which version of draw() does the compiler call?
In fact the compiler doesn’t know what to do, so it arranges for the decision to
be deferred until the program is running.
At runtime, when it is known what class is pointed to by ptr, the appropriate
version of draw will be called. This is called late binding or dynamic binding.
Choosing functions in the normal way, during compilation, is called early
binding or static binding.
Late binding requires some overhead but provides increased power and
flexibility.
We’ll put these ideas to use in a moment, but first let’s consider a refinement
to the idea of virtual functions.
11. Abstract Classes and Pure Virtual Functions
Think of the shape class. We’ll never make an object
of the shape class; we’ll only make specific shapes
such as circles and triangles.
When we will never want to instantiate objects of a
base class, we call it an abstract class. Such a class exists only to act as a
parent of derived classes that will be used to instantiate objects.
If we don’t want anyone to instantiate objects of the base class, we’ll write
our classes so that such instantiation is impossible.
How can we can do that? By placing at least one pure virtual function in the
base class.
A pure virtual function is one with the expression =0 added to the declaration.
This is shown in the next example.
Shape
Triangle
Rectangle Square
12. #include <iostream>
using namespace std;
class Base { // base class
public: virtual void show() = 0;
// pure virtual function
// Using =0 syntax we tell the compiler
// that a virtual function will be pure.
// this is only a declaration, you never
// need to write a definition of the base
// class show(), although you can if you
// need to.
};
class Derv1 : public Base {
public:
void show() {
cout << "Derv1 n";
}
}; 12
Virtual Member Functions Accessed with Pointer
class Derv2 : public Base {
public:
void show() {
cout << "Derv 2n";
}
};
int main() {
// you can’t make object from abstract class
// Base bad;
Base* arr[2]; // array of base class pointers
Derv1 dv1; // object of derived class 1
Derv2 dv2; // object of derived class 2
arr[0] = &dv1; // put address of dv1 in array
arr[1] = &dv2; // put address of dv2 in array
arr[0]->show(); // execute show() in objects
arr[1]->show();
return 0;
} 2
1
13. Abstract Classes and Pure Virtual Functions
Once you’ve placed a pure virtual function in the base class, you must override
it in all the derived classes from which you want to instantiate objects.
If a class doesn’t override the pure virtual function, it becomes an abstract
class itself, and you can’t instantiate objects from it (although you might from
classes derived from it).
For consistency, you may want to make all the virtual functions in the base
class pure.
14. #include <iostream>
using namespace std;
class person // person class
{
protected:
char name[40];
public:
void getName() {
cout << " Enter name: ";
cin >> name;
}
void putName() {
cout << "Name is: " << name << endl;
}
// two pure virtual functions
virtual void getData() = 0;
virtual bool isOutstanding() = 0;
};
14
Virtual Functions and the Person Class
class student : public person
{
private:
float gpa; // grade point average
public:
void getData() // get student data from user
{
person::getName();
cout << " Enter student's GPA: ";
cin >> gpa;
}
bool isOutstanding()
{
return (gpa > 3.5);
}
};
1 2
15. class professor : public person {
// number of papers published
private: int numPubs;
public:
void getData() { //get professor data
person::getName();
cout << "Enter number of professor's"
"publications: ";
cin >> numPubs;
}
bool isOutstanding() {
return (numPubs > 100);
}
};
int main() {
person* Ptr[100]; // array of pointers
int n = 0;
char choice;
15
Virtual Functions and the Person Class
do {
cout << "Enter student or professor (s/p): ";
cin >> choice;
if(choice=='s') Ptr[n] = new student;
else Ptr[n] = new professor;
Ptr[n++]->getData(); //get person data
cout << " Enter another (y/n)? ";
cin >> choice;
fflush(stdin);
} while( choice=='y' ); //cycle until not ‘y’
for(int j=0; j<n; j++){ //print names of all
Ptr[j]->putName(); // say if outstanding
if( Ptr[j]->isOutstanding() )
cout << " This person is outstandingn";
}
system("PAUSE"); return 0;
} //end main()
3 4
17. 17
Virtual Functions UML Diagram
Customer
Name
Bank
Name
Account
balance
virtual Credit()
virtual Debit()
CurrentAccount
Credit()
Debit()
SavingAccount
interestRate
Credit()
Debit()
AddInterest()
18. Virtual Destructors
For consistency, you may want to make all the virtual functions in the base
class pure.
Base class destructors should always be virtual.
Suppose you use delete with a base class pointer to a derived class object to
destroy the derived-class object.
If the base-class destructor is not virtual then delete, like a normal member
function, calls the destructor for the base class, not the destructor for the
derived class.
This will cause only the base part of the object to be destroyed.
The next program shows how this looks.
19. #include <iostream>
#include <stdlib.h>
using namespace std;
class Base {
public:
// ~Base() // non-virtual destructor
virtual ~Base() // virtual destructor
{
cout << "Base destroyedn";
}
};
class Derv : public Base {
public:
~Derv() {
cout << "Derv destroyedn";
}
};
19
Virtual Functions and the Person Class
int main() {
Base* pBase = new Derv;
delete pBase;
system("PAUSE");
return 0;
}
1 2
Using Non-virtual Destructor
Using Virtual Destructor
20. Virtual Base Classes
Before leaving the subject of virtual programming elements, we should
mention virtual base classes as they relate to multiple inheritance.
Consider the situation shown in Fig. with a base class, Parent; two derived
classes, Child1 and Child2; and a fourth class, Grandchild, derived from both
Child1 and Child2.
In this arrangement a problem can arise
if a member function in the Grandchild class
wants to access data or functions in the
Parent class.
The next program shows what happens.
21. #include <iostream>
#include <stdlib.h>
using namespace std;
class Parent {
protected:
int basedata;
};
// virtual public
class Child1 : public Parent { };
// virtual public
class Child2 : public Parent { };
21
Virtual Base Classes
class Grandchild : public Child1, public Child2 {
public:
void getdata() {
cin >> basedata; // ERROR: ambiguous
}
void putdata() {
cout << basedata << endl; // Error
}
};
int main() {
Grandchild c1;
c1.getdata();
c1.putdata();
system("PAUSE");
return 0;
}
1 2
The use of the keyword virtual in these two
classes causes them to share a single common
subobject of their base class Parent. Since
there is only one copy of basedata, there is no
ambiguity when it is referred to in Grandchild.
22. Friend Functions
The concepts of encapsulation and data hiding dictate that nonmember
functions should not be able to access an object’s private or protected data
The policy is, if you’re not a member, you can’t get in.
However, there are situations where such rigid discrimination leads to
considerable inconvenience.
Imagine that you want a function to operate on objects of two different
classes.
Perhaps the function will take objects of the two classes as arguments, and
operate on their private data.
In this situation there’s nothing like a friend function.
The next example shows how that friend functions can act as a bridge
between two classes:
23. #include <iostream>
#include <stdlib.h>
using namespace std;
class beta;// for frifunc declaration
class alpha {
private:
int data;
public:
alpha() : data(3) { }
friend int frifunc(alpha, beta);
};
class beta {
private:
int data;
public:
beta() : data(7) { }
friend int frifunc(alpha, beta);
};
23
Friends as Bridges
int frifunc(alpha a, beta b)
{
return( a.data + b.data );
}
int main()
{
alpha aa; beta bb;
// call the function
cout << frifunc(aa, bb) << endl;
system("PAUSE");
return 0;
}
1 2
24. #include <iostream>
#include <stdlib.h>
using namespace std;
class Distance {// English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0){}
Distance(float fltfeet){ //convert float
//feet is integer part
feet = static_cast<int>(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in)
{ feet = ft; inches = in; }
void showdist(){
cout<<feet<<"'-" <<inches<<'"';
}
Distance operator + (Distance);
};
24
English Distance Example
Distance Distance::operator + (Distance d2) {
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) // if total exceeds 12.0,
{ i -= 12.0; f++; }
// return new Distance with sum
return Distance(f,i);
}
int main() {
Distance d1 = 2.5, d2 = 1.25, d3;
cout << "nd1 = "; d1.showdist();
cout << "nd2 = "; d2.showdist();
d3 = d1 + 10.3F;
cout << "nd3 = "; d3.showdist();
// d3 = 10.0 + d1; //float + Distance: ERROR
cout << endl;
system("PAUSE");
return 0;
}
1 2
25. English Distance Example
In this program, the + operator is overloaded to add two objects of type
Distance.
Also, there is a one-argument constructor that converts a value of type float,
representing feet and decimal fractions of feet, into a Distance value. (it
converts 10.25' into 10'–3''.)
When such a constructor exists, you can make statements like this in main():
d3 = d1 + 10.0;
The overloaded + is looking for objects of type Distance both on its left and on
its right, but if the argument on the right is type float, the compiler will use
the one-argument constructor to convert this float to a Distance value, and
then carry out the addition.
The statement: d3 = 10.0 + d1; does not work, because the object of which
the overloaded + operator is a member must
26. English Distance Example
be the variable to the left of the operator.
When we place a variable of a different type there, or a constant, then
the compiler uses the + operator that adds that type (float in this case),
not the one that adds Distance objects.
Unfortunately, this operator does not know how to convert
float to Distance, so it can’t handle this situation.
How can we write natural-looking statements that have nonmember
data types to the left of the operator?
As you may have guessed, a friend can help you out of this dilemma.
The FRENGL program shows how.
27. #include <iostream>
#include <stdlib.h>
using namespace std;
class Distance {// English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0){}
Distance(float fltfeet){ //convert float
//feet is integer part
feet = static_cast<int>(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in)
{ feet = ft; inches = in; }
void showdist(){
cout<<feet<<"'-" <<inches<<'"';
}
friend Distance operator + (Distance,
Distance);
};
27
English Distance Example Friend Operator
Distance operator + (Distance d1, Distance d2) {
int f = d1.feet + d2.feet; //add the feet
float i = d1.inches + d2.inches; //add inches
if(i >= 12.0) //if inches exceeds 12.0,
{ i -= 12.0; f++; }
//return new Distance with sum
return Distance(f,i);
}
int main(){
Distance d1 = 2.5, d2 = 1.25, d3;
cout << "nd1 = "; d1.showdist();
cout << "nd2 = "; d2.showdist();
d3 = d1 + 10.0; //distance + float: OK
cout << "nd3 = "; d3.showdist();
d3 = 10.3F + d1; //float + Distance: OK
cout << "nd3 = "; d3.showdist(); cout << endl;
system("PAUSE");
return 0;
}
1 2
28. English Distance Example
The overloaded + operator is made into a friend:
friend Distance operator + (Distance, Distance);
Notice that, while the overloaded + operator took one argument as a
member function, it takes two as a friend function.
In a member function, one of the objects on which the + operates is the
object of which it was a member, and the second is an argument.
In a friend, both objects must be arguments. The only change to the
body of the overloaded + function is that the variables feet and inches,
used in NOFRI for direct access to the object’s data, have been
replaced in FRENGL by d1.feet and d1.inches, since this object is
supplied as an argument.
29. English Distance Example
Sometimes a friend allows a more obvious syntax for calling a function
than does a member function.
For example, suppose we want a function that will square (multiply by
itself) an object of the English Distance class and return the result in
square feet, as a type float.
The MISQ example shows how this might be done with a member
function.
30. #include <iostream>
#include <stdlib.h>
using namespace std;
class Distance { // English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0) { }
Distance(int ft, float in) : feet(ft),
inches(in) { }
void showdist() { // display distance
cout<<feet<<"'-"<<inches<<'"';
}
float square(); // member function
};
float Distance::square(){
float fltfeet = feet + inches/12;
float feetsqrd = fltfeet * fltfeet;
return feetsqrd; //return square feet
}
30
Friend for Functional Notation
int main() {
Distance dist(3, 6.0);
float sqft;
sqft = dist.square(); // return square of dist
// sqft = square(dist);
// But this is more natural way We can achieve
// this effect by making square() a friend of
// the Distance class
// display distance and square
cout << "nDistance = "; dist.showdist();
cout << "nSquare = " <<sqft<<" square feetn";
system("PAUSE");
return 0;
}
1 2
32. #include <iostream>
#include <stdlib.h>
using namespace std;
class alpha{
private: int data1;
public:
alpha() : data1(99) { } // constructor
friend class beta;
// (friend beta) is a friend class
};
32
Friend Class
class beta{
// all member functions can
public: // access private alpha data
void func1(alpha a) {
cout << "ndata1=" << a.data1;
}
void func2(alpha a) {
cout << "ndata1=" << a.data1;
}
};
int main(){
alpha a; beta b;
b.func1(a);b.func2(a);cout << endl;
system("PAUSE");
return 0;
}
1 2
33. Static Function
A static data member is not duplicated for each object; rather a single data
item is shared by all objects of a class.
The STATIC example showed a class that kept track of how many objects of
itself there were.
Let’s extend this concept by showing how functions as well as data may be
static.
Next program models a class that provides an ID number for each of its
objects.
So, you to query an object to find out which object it is.
The program also casts some light on the operation of destructors.
Here’s the listing for STATFUNC:
[33]
34. #include <iostream>
#include <stdlib.h>
using namespace std;
class gamma{
private: static int total;
// total objects of this class
// (declaration only)
int id; // ID number of this object
public:
gamma() { total++; id = total; }
~gamma() { total--;
cout << "Destroying ID " <<id<<endl; }
static void showtotal()
{ cout << "Total is " << total << endl; }
void showid() //non-static function
{ cout << "ID number is " <<id<< endl; }
};
Static Function
int gamma::total = 0; // definition of total
int main(){
gamma g1;
gamma::showtotal();
// To access showtotal() using only
// the class name, we must declare it to be a
// static member function
// there may be no such objects at all
gamma g2, g3;
gamma::showtotal();
g1.showid();
g2.showid();
g3.showid();
gamma * g4 = new gamma;
g4->showid();
delete g4;
system("PAUSE");
return 0;
}
1 2
[34]
35. #include <iostream>
using namespace std;
class alpha{
private: int data;
public:
alpha() { }
alpha(int d) { data = d; }
void display(){ cout << data; }
alpha operator = (alpha& a) {
// not done automatically
data = a.data;
cout << "Assignment operator invoked";
// return copy of this alpha
return alpha(data);
}
};
Overloading the Assignment = Operator
int main() {
alpha a1(37);
alpha a2;
a2 = a1; // invoke overloaded =
cout << "na2="; a2.display(); // display a2
alpha a3 = a2; // is not an assignment but an
// initialization, with the same effect
alpha a3(a2); // it does NOT invoke =
// we initialize the object a3 to the value a2
cout << "na3="; a3.display(); // display a3
cout << endl;
return 0;
}
1 2
[35]
36. Overloading the Assignment Operator
The assignment operator = is unique among operators in that it is not inherited.
If you overload the assignment operator in a base class, you can’t use this same function in
any derived classes.
you can define and at the same time initialize an object to the value of another object with
two kinds of statements:
Both styles of definition invoke a copy constructor: A constructor that creates a new
object and copies its argument into it.
The default copy constructor, which is provided automatically by the compiler for every
object, performs a member-by-member copy. This is similar to what the assignment
operator does; the difference is that the default copy constructor also creates a new object.
Like the assignment operator, the copy constructor can be overloaded by the user.
[36]
alpha a3(a2); // copy initialization
alpha a3 = a2; // copy initialization, alternate syntax
38. Overloading the Copy Constructor
The copy constructor is invoked when an object is passed by value to a function. It creates
the copy that the function operates on.
Thus if the function void func(alpha); were declared in the program and this function were
called by the statement func(a1); then the copy constructor would be invoked to create a
copy of the a1 object for use by func().
Of course, the copy constructor is not invoked if the argument is passed by reference or if
a pointer to it is passed. In these cases no copy is created; the function operates on the
original variable.
The copy constructor also creates a temporary object when a value is returned from a
function. Suppose there was a function like this alpha func(); and this function was called by
the statement a2 = func();
The copy constructor would be invoked to create a copy of the value returned by func(), and
this value would be assigned (invoking the assignment operator) to a2.
When an argument is passed by value, a copy of it is constructed. What makes the copy?
The copy constructor. But this is the copy constructor, so it calls itself.
[38]
39. How to Prohibit Copying
To avoid copying, overload the assignment operator and the copy constructor as private
members.
As soon as you attempt a copying operation, such as
The compiler will tell you that the function is not accessible. You don’t need to define the
functions, since they will never be called.
[39]
class alpha{
private:
alpha& operator = (alpha&); // private assign. Operator
alpha(alpha&); // private copy constructor
};
alpha a1, a2;
a1 = a2; // assignment
alpha a3(a1); // copy constructor
40. #include <iostream>
using namespace std;
class array{
private:
// occupies 10 bytes
char charray[10];
public:
void reveal() {
cout << "nMy object’s address is "
<< this;
}
};
int main(){
array w1, w2, w3; // make three objects
w1.reveal(); // see where they are
w2.reveal();
w3.reveal();
cout << endl;
return 0;
}
The this Pointer
1 2
[40]
The main() program in this example creates three
objects of type where.
It then asks each object to print its address, using the
reveal() member function.
This function prints out the value of the this pointer.
Here’s the output:
My object’s address is 0x8f4effec
My object’s address is 0x8f4effe2
My object’s address is 0x8f4effd8
Since the data in each object consists of an array of 10
bytes, the objects are spaced 10 bytes apart in memory.
Some compilers may place extra bytes in objects, making
them slightly larger than 10 bytes.
41. // dothis.cpp the this pointer referring
to data
#include <iostream>
using namespace std;
class what{
private: int alpha;
public:
void tester(){
// same as alpha = 11;
this->alpha = 11;
//same as cout << alpha;
cout << this->alpha;
}
};
int main(){
what w; w.tester();
cout << endl; return 0;
}
Accessing Member Data with this
1 2
[41]
This program simply prints out the value 11.
Notice that the tester() member function accesses the
variable alpha as this->alpha
This is exactly the same as referring to alpha directly.
This syntax works, but there is no reason for it except to
show that this does indeed point to the object.
42. Using this for Returning Values
A more practical use for this is in returning values from member functions and overloaded
operators.
Recall that in the program where we could not return an object by reference, because the
object was local to the function returning it and thus was destroyed when the function
returned.
We need a more permanent object if we’re going to return it by reference. The object of
which a function is a member is more permanent than its individual member functions.
An object’s member functions are created and destroyed every time they’re called, but the
object itself endures until it is destroyed by some outside agency (for example, when it is
deleted).
Thus returning by reference the object of which a function is a member is a better bet than
returning a temporary object created in a member function. The this pointer makes this
easy.
[42]
43. //assign2.cpp returns contents of the this pointer
#include <iostream>
using namespace std;
class alpha{
private: int data;
public:
alpha() { } // no-arg constructor
alpha(int d) { data = d; } // one-arg constructor
void display() { cout << data; } // display data
alpha& operator = (alpha& a){ // overloaded = operator
data = a.data; // not done automatically
cout << "nAssignment operator invoked";
return *this; // return copy of this alpha
}
};
int main(){
alpha a1(37); alpha a2, a3;
a3 = a2 = a1; //invoke overloaded =, twice
cout << "na2="; a2.display(); cout << "na3="; a3.display(); //display a3
cout << endl; return 0; }
Using this for Returning Values
[43]
44. Using this for Returning Values
In this program we can use the declaration alpha& operator = (alpha& a) which returns by
reference, instead of alpha operator = (alpha& a) which returns by value.
The last statement in this function is return *this;
Since this is a pointer to the object of which the function is a member, *this is that object
itself, and the statement returns it by reference. Here’s the output of ASSIGN2:
Assignment operator invoked
Assignment operator invoked
a2=37
a3=37
Each time the equal sign is encountered in a3 = a2 = a1; the overloaded operator=()
function is called, which prints the messages. The three objects all end up with the same
value.
You usually want to return by reference from overloaded assignment operators, using *this,
to avoid the creation of extra objects. [44]
45. Beware of Self-Assignment
A corollary of Murphy’s Law states that whatever is possible, someone will eventually do.
This is certainly true in programming, so you can expect that if you have overloaded the =
operator, someone will use it to set an object equal to itself: alpha = alpha;
Your overloaded assignment operator should be prepared to handle such self-assignment.
Otherwise, bad things may happen
[45]
46. Checking the Type of a Class with dynamic_cast
Suppose some other program sends your program an object (as the operating
system might do with a callback function). It’s supposed to be a certain type of
object, but you want to check it to be sure.
How can you tell if an object is a certain type?
The dynamic_cast operator provides a way, assuming that the classes whose
objects you want to check are all descended from a common ancestor. The
DYNCAST1 program shows how this looks.
[46]
47. // RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for dynamic_cast
using namespace std;
class Base{
virtual void vertFunc() //needed for dynamic cast
{ }
};
class Derv1 : public Base { };
class Derv2 : public Base { };
//checks if pUnknown points to a Derv1
bool isDerv1(Base* pUnknown){ //unknown subclass of Base
Derv1* pDerv1;
if( pDerv1 = dynamic_cast<Derv1*>(pUnknown) )
return true;
else
return false;
}
//--------------------------------------------------------------
int main(){
Derv1* d1 = new Derv1;
Checking the Type of a Class with dynamic_cast
[47]
48. Checking the Type of a Class with dynamic_cast
Here we have a base class Base and two derived classes Derv1 and Derv2.
There’s also a function, isDerv1(), which returns true if the pointer it received as an
argument points to an object of class Derv1.
This argument is of class Base, so the object passed can be either Derv1 or Derv2.
The dynamic_cast operator attempts to convert this unknown pointer pUnknown to
type Derv1.
If the result is not zero, pUnknown did point to a Derv1 object.
If the result is zero, it pointed to something else.
The dynamic_cast operator allows you to cast upward and downward in the
inheritance tree. However, it allows such casting only in limited ways.
The DYNCAST2 program shows examples of such casts.
[48]
49. // RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for dynamic_cast
using namespace std;
////////////////////////////////////////////////////////////////
class Base {
protected:
int ba;
public:
Base() : ba(0)
{ }
Base(int b) : ba(b)
{ }
virtual void vertFunc() //needed for dynamic_cast
{ }
void show()
{ cout << "Base: ba=" << ba << endl; }
};
////////////////////////////////////////////////////////////////
class Derv : public Base
{
Changing Pointer Types with dynamic_cast
[49]
50. The typeid Operator
Sometimes you want more information about an object than simple verification that
it’s of a certain class.
You can obtain information about the type of an unknown object, such as its class
name, using the typeid operator.
The TYPEID program demonstrates how it works.
[50]
51. // typeid.cpp
// demonstrates typeid() function
// RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for typeid()
using namespace std;
////////////////////////////////////////////////////////////////
class Base
{
virtual void virtFunc() //needed for typeid
{ }
};
class Derv1 : public Base
{ };
class Derv2 : public Base
{ };
////////////////////////////////////////////////////////////////
void displayName(Base* pB)
{
cout << "pointer to an object of "; //display name of class
cout << typeid(*pB).name() << endl; //pointed to by pB
The typeid Operator
[51]
52. The typeid Operator
In this example the displayName() function displays the name of the class of the
object passed to it. To do this, it uses the name member of the type_info class,
along with the typeid operator.
In main() we pass this function two objects of class Derv1 and Derv2 respectively,
and the program’s output is
pointer to an object of class Derv1
pointer to an object of class Derv2
Besides its name, other information about a class is available using typeid. For
example, you can check for equality of classes using an overloaded == operator.
We’ll show an example of this in the EMPL_IO program in Chapter 12, “Streams
and Files.” Although the examples in this section have used pointers, dynamic_cast
and typeid work equally well with references.
[52]