The document discusses class and object-oriented programming concepts such as classes, objects, member functions, and constructors. It provides examples of class declarations and definitions, and how to create objects and call member functions. The document also covers constructors, initializer lists, destructors, function templates, and class templates.
In JS: CLASS <=> Constructor FN
new FN() => FN() { this }
FN = CLASS (FN = FN, FN = DATA)
Objects
Prototype / __proto__
Inheritence
Rewriting / Augmenting
built in objects
Android Developer Group Poznań - Kotlin for Android developers
STXInsider example project in Kotlin:
https://github.com/kosiara/stx-insider
Kotlin - one of the popular programming languages built on top of Java that runs on JVM. Thanks to JetBrains support and excellent IDE integration, it’s an ideal choice for Android development. 100% Java compatibility, interoperability and no runtime overhead is just the beginning of a long list of strengths. Kotlin is supposed to be a subset of SCALA, has clear benefits for developers on one hand and keeps short compile times on the other.
As a mobile team we got interested in Kotlin a few months before its final release which gave us time to test it thoroughly before production use. The language has some clear advantages for an Android programmer - it enables migration from Java projects that have been under development for some time already. Java&Kotlin coexistence simplifies Kotlin introduction as only new functionality is written in JetBrain’s new language leaving all the legacy code untouched.
Transitioning gives the developer an opportunity to use lambdas, new syntax for data objects, extension functions to easily expand Android SDK’s classes functionality and infix notation to write DSL-like structures. Almost all the libraries you use today will work with Kotlin thanks to 100% Java compatibility. The same is true for Android SDK classes - all of them will seamlessly work with the new programming language. Kotlin gives you more choice when it comes to reflection, creating documentation and being null-pointer safe. Android works great with it out of the box so you won’t need to change your development habits.
Our production project in Kotlin turned out to be a success after 4 months of development. We had 0 bugs related to Kotlin as a programming language. Our code footprint is almost 30% smaller thanks to JetBrain’s, we benefit from nullpointer safety, closures, translated enums, data objects and use infix notation for logging and displaying Snackbars.
===========
In this presentation you'll find basic use cases, syntax, structures and patterns. Later on Kotlin is presented in Android context. Simple project structure, imports and Kotlin usage with Android SDK is explained. In the end cost of Kotlin compilation is presented and the language is compared to SCALA and SWIFT.
We look at the positive impact new syntax can have on boilerplate removal and readability improvement.
Kotlin really shines in Android development when one looks at “Enum translation”, “Extension functions”, “SAM conversions”, “Infix notation”, “Closures” and “Fluent interfaces” applied to lists. The talk, however, compares language-specifics of Java & Kotlin in terms of “Type Variance”, “Generics” and “IDE tools” as well.
Learn about the basic fundamentals of java and important for the different company's interview. Topics like JRE, JDK, Java Keywords, Primitive DataTypes, Types of Variables, Logical, Shift and Bitwise Operator working, Command Line Argument, Handling Arrays, Array Copy, and different programs and output based programs.
Since these presentations were spare time hobby - I've decided to share them :)
Hopefully someone will find them useful.
This part continues 1. part with more design patterns like Command, State, NullObject.
Powerful JavaScript Tips and Best PracticesDragos Ionita
Javascript Best Practices, IIFE, Immediately invoked function expressions, typeof, instanceof, constructor, random items from an array, generate an array of numbers, append an array to another array, is a number, is an array, remove an item from an array
Modul untuk Praktikum Pengolahan Database di Java dengan menggunakan konsep OOP Mata Pelajaran Pemrograman Internet dengan Java-JSP SMK Telkom Sandhy Putra Malang
IDX Broker is a real estate data and web software company. Barrett Avery, Data Systems Lead at IDX, will talk about the change in programming philosophy our data team is currently going through. He’ll talk about where we’ve been, where we’re going and why. Other topics he’ll cover include S.O.L.I.D. programming methodology and the Laravel PHP framework, and the pros and cons of each when dealing with large amounts of data.
In JS: CLASS <=> Constructor FN
new FN() => FN() { this }
FN = CLASS (FN = FN, FN = DATA)
Objects
Prototype / __proto__
Inheritence
Rewriting / Augmenting
built in objects
Android Developer Group Poznań - Kotlin for Android developers
STXInsider example project in Kotlin:
https://github.com/kosiara/stx-insider
Kotlin - one of the popular programming languages built on top of Java that runs on JVM. Thanks to JetBrains support and excellent IDE integration, it’s an ideal choice for Android development. 100% Java compatibility, interoperability and no runtime overhead is just the beginning of a long list of strengths. Kotlin is supposed to be a subset of SCALA, has clear benefits for developers on one hand and keeps short compile times on the other.
As a mobile team we got interested in Kotlin a few months before its final release which gave us time to test it thoroughly before production use. The language has some clear advantages for an Android programmer - it enables migration from Java projects that have been under development for some time already. Java&Kotlin coexistence simplifies Kotlin introduction as only new functionality is written in JetBrain’s new language leaving all the legacy code untouched.
Transitioning gives the developer an opportunity to use lambdas, new syntax for data objects, extension functions to easily expand Android SDK’s classes functionality and infix notation to write DSL-like structures. Almost all the libraries you use today will work with Kotlin thanks to 100% Java compatibility. The same is true for Android SDK classes - all of them will seamlessly work with the new programming language. Kotlin gives you more choice when it comes to reflection, creating documentation and being null-pointer safe. Android works great with it out of the box so you won’t need to change your development habits.
Our production project in Kotlin turned out to be a success after 4 months of development. We had 0 bugs related to Kotlin as a programming language. Our code footprint is almost 30% smaller thanks to JetBrain’s, we benefit from nullpointer safety, closures, translated enums, data objects and use infix notation for logging and displaying Snackbars.
===========
In this presentation you'll find basic use cases, syntax, structures and patterns. Later on Kotlin is presented in Android context. Simple project structure, imports and Kotlin usage with Android SDK is explained. In the end cost of Kotlin compilation is presented and the language is compared to SCALA and SWIFT.
We look at the positive impact new syntax can have on boilerplate removal and readability improvement.
Kotlin really shines in Android development when one looks at “Enum translation”, “Extension functions”, “SAM conversions”, “Infix notation”, “Closures” and “Fluent interfaces” applied to lists. The talk, however, compares language-specifics of Java & Kotlin in terms of “Type Variance”, “Generics” and “IDE tools” as well.
Learn about the basic fundamentals of java and important for the different company's interview. Topics like JRE, JDK, Java Keywords, Primitive DataTypes, Types of Variables, Logical, Shift and Bitwise Operator working, Command Line Argument, Handling Arrays, Array Copy, and different programs and output based programs.
Since these presentations were spare time hobby - I've decided to share them :)
Hopefully someone will find them useful.
This part continues 1. part with more design patterns like Command, State, NullObject.
Powerful JavaScript Tips and Best PracticesDragos Ionita
Javascript Best Practices, IIFE, Immediately invoked function expressions, typeof, instanceof, constructor, random items from an array, generate an array of numbers, append an array to another array, is a number, is an array, remove an item from an array
Modul untuk Praktikum Pengolahan Database di Java dengan menggunakan konsep OOP Mata Pelajaran Pemrograman Internet dengan Java-JSP SMK Telkom Sandhy Putra Malang
IDX Broker is a real estate data and web software company. Barrett Avery, Data Systems Lead at IDX, will talk about the change in programming philosophy our data team is currently going through. He’ll talk about where we’ve been, where we’re going and why. Other topics he’ll cover include S.O.L.I.D. programming methodology and the Laravel PHP framework, and the pros and cons of each when dealing with large amounts of data.
Introduction to Object Oriented ProgrammingMoutaz Haddara
An Introduction to Object-Oriented Programming (OOP)
Download the presentation to view it correctly, as it has some animations that won't show here.
If you have any questions, please contact me. You are free to use it this presentation, but it would be nice at least to give me some credit :)
Content:
1- History of Programming
2. Objects and Classes
3- Abstraction, Inheritance, Encapsulation, and Polymorphism
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
Certainly, here's a brief explanation of constructors, destructors, and operator overloading without using code:
Constructors: Constructors are special methods used to initialize objects of a class. They set the initial state of an object when it is created.
Destructors: Destructors are special methods used to clean up resources and perform necessary cleanup when an object is no longer needed or goes out of scope. They ensure proper resource management.
Operator Overloading: Operator overloading is a feature that allows you to define custom behaviors for operators such as +, -, *, /, etc., when they are applied to objects of your class. It enables you to work with objects in a way that is meaningful for your specific class.
Constructors can be of different types:
Default Constructors: Initialize objects with default values.
Parameterized Constructors: Accept arguments to initialize objects with specific values.
Copy Constructors: Create a new object as a copy of an existing object.
Constructor Overloading: A class can have multiple constructors with different parameter lists, providing flexibility in object initialization.
Destructors are executed automatically when an object is destroyed. They are essential for releasing resources like memory, file handles, or network connections, ensuring proper cleanup and preventing resource leaks.
Operator overloading enables you to define how operators work with objects of your class. For instance, you can specify what the + operator does when applied to two objects of your class, allowing for custom operations that make sense in the context of your class's functionality.
In summary, constructors initialize objects, destructors handle cleanup, and operator overloading allows custom operations with operators when working with objects. These features are crucial for building custom classes in object-oriented programming.
Download this Presentation for free from www.ecti.co.in/downloads.html
No SIGN UP REQUIRED.
C++ Programming Training PPTs / PDFs for free.
Download free C++ Programming study material. Learn C++ Programming for free in 2 hours.
THIS IS COMPELTE VARIABLE POWER SUPPLY PROJECT, HELP YOU YOU TO UNDERSTAND. WE DESIGNED THE CIRCUIT ON PROTEUS AND ITS PICTURE IS IN PROTEUS.IT WILL GIVE YOU BOTH POSITIVE AND NEGATIVE VOLTAGE.
Ethnobotany and Ethnopharmacology:
Ethnobotany in herbal drug evaluation,
Impact of Ethnobotany in traditional medicine,
New development in herbals,
Bio-prospecting tools for drug discovery,
Role of Ethnopharmacology in drug evaluation,
Reverse Pharmacology.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
Andreas Schleicher presents at the OECD webinar ‘Digital devices in schools: detrimental distraction or secret to success?’ on 27 May 2024. The presentation was based on findings from PISA 2022 results and the webinar helped launch the PISA in Focus ‘Managing screen time: How to protect and equip students against distraction’ https://www.oecd-ilibrary.org/education/managing-screen-time_7c225af4-en and the OECD Education Policy Perspective ‘Students, digital devices and success’ can be found here - https://oe.cd/il/5yV
The Indian economy is classified into different sectors to simplify the analysis and understanding of economic activities. For Class 10, it's essential to grasp the sectors of the Indian economy, understand their characteristics, and recognize their importance. This guide will provide detailed notes on the Sectors of the Indian Economy Class 10, using specific long-tail keywords to enhance comprehension.
For more information, visit-www.vavaclasses.com
The Roman Empire A Historical Colossus.pdfkaushalkr1407
The Roman Empire, a vast and enduring power, stands as one of history's most remarkable civilizations, leaving an indelible imprint on the world. It emerged from the Roman Republic, transitioning into an imperial powerhouse under the leadership of Augustus Caesar in 27 BCE. This transformation marked the beginning of an era defined by unprecedented territorial expansion, architectural marvels, and profound cultural influence.
The empire's roots lie in the city of Rome, founded, according to legend, by Romulus in 753 BCE. Over centuries, Rome evolved from a small settlement to a formidable republic, characterized by a complex political system with elected officials and checks on power. However, internal strife, class conflicts, and military ambitions paved the way for the end of the Republic. Julius Caesar’s dictatorship and subsequent assassination in 44 BCE created a power vacuum, leading to a civil war. Octavian, later Augustus, emerged victorious, heralding the Roman Empire’s birth.
Under Augustus, the empire experienced the Pax Romana, a 200-year period of relative peace and stability. Augustus reformed the military, established efficient administrative systems, and initiated grand construction projects. The empire's borders expanded, encompassing territories from Britain to Egypt and from Spain to the Euphrates. Roman legions, renowned for their discipline and engineering prowess, secured and maintained these vast territories, building roads, fortifications, and cities that facilitated control and integration.
The Roman Empire’s society was hierarchical, with a rigid class system. At the top were the patricians, wealthy elites who held significant political power. Below them were the plebeians, free citizens with limited political influence, and the vast numbers of slaves who formed the backbone of the economy. The family unit was central, governed by the paterfamilias, the male head who held absolute authority.
Culturally, the Romans were eclectic, absorbing and adapting elements from the civilizations they encountered, particularly the Greeks. Roman art, literature, and philosophy reflected this synthesis, creating a rich cultural tapestry. Latin, the Roman language, became the lingua franca of the Western world, influencing numerous modern languages.
Roman architecture and engineering achievements were monumental. They perfected the arch, vault, and dome, constructing enduring structures like the Colosseum, Pantheon, and aqueducts. These engineering marvels not only showcased Roman ingenuity but also served practical purposes, from public entertainment to water supply.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
2. Outline
Class and Object
Constructor
Constructor Initializer List
Destructor
Default Initialization Values
Function Templates
Class Templates
2
3. Class
A class is a programmer-defined data type. It
consists of data and functions which
operate on that data.
Placing data and functions together into a
single entity is the central idea of object-
oriented programming.
3
4. Class Declaration Syntax
class Name // usually capitalized
{
public:
public members; // usually functions
private:
private members; // usually variables
};
Note
4
5. Example of a Class Declaration
#include<iostream>
using namespace std;
class ASimpleClass //declare a class
{
private:
int data; //a private member
public:
void setData(int item)
{
data = item;
}
void displayData()
{
cout<<"Data is : "<<data<<endl;
}
}; //program continues … 5
6. Example of a Class Declaration
void main()
{
ASimpleClass asp; //creates an object
asp.setData(10); //member access using dot operator
asp.displayData();
asp.setData(20);
asp.displayData();
}
Output
Data is : 10
Data is : 20
Press any key to continue
6
7. A Few Terms
class
class member
data member
member function
class object (class instance)
7
8. Objects
An object is an instance of a class.
Similarity:
int row;
This creates an instance of int called “row”.
8
9. Calling Member Functions
asp.setData(10);
asp.displayData();
These two statements are not like normal function calls.
This strange syntax is used to call a member function that is
associated with a specific object.
A member function must always be called in connection with an
object of the class.
Because a member function is always called to act on a specific
object, and not on the class in general.
The dot operator connects the object name and the member
function.
Dot operator is also called class member access operator.
9
10. Working with Objects
ASimpleClass asp1,asp2;
asp1.setData(10);
asp2.setData(20);
ASimpleClass
int data
void setData();
void displayData();
Two objects of ASimpleClass
data data
10 20
asp1 asp2
10
11. Part Example
#include<iostream> partno=pn;
using namespace std; cost=c;
class Part }
{ void showPart()
{
int modelno;
cout<<"Model :
int partno; "<<modelno<<endl;
float cost; cout<<"Part :
public: "<<partno<<endl;
void setPart(int mn, int pn, cout<<"Cost : "<<cost<<endl;
float c) }
{ }; //program continues …
modelno=mn;
//continues on right side… 11
12. Part Example Cont…
void main()
{
Part part;
part.setPart(555,100,100);
part.showPart();
}
Output
Model : 555
Part : 100
Cost : 100
Press any key to continue
12
13. Constructor
Setters provide a way of initializing class data
members.
However, sometimes it is convenient that an object
can initialize itself when it is created, without the
need to call setters.
13
14. Constructor
Automatic initialization is carried out using a special
member function called a constructor.
A constructor is a special member function provided
to allow initialization of variables.
Constructor is executed automatically whenever an
object is created.
14
15. Counter Example (with constructor)
//counter.h
class Counter
{
private:
unsigned int count;
public:
Counter() {count=0; } //constructor
void incCount();
unsigned int getCount();
};
15
16. Counter Example (with constructor)
//counter.cpp
#include"counter.h"
unsigned int Counter::getCount()
{
return count;
}
void Counter::incCount()
{
count++;
}
16
17. Counter Example (with constructor)
//driver.cpp
#include"counter.h"
#include<iostream>
using namespace std;
void main() {
Counter c; //creates an object and initializes count
cout<<c.getCount()<<endl;
c.incCount();
cout<<c.getCount()<<endl;
}
17
18. Constructor
Must have the same name as the class itself
Name is one way the compiler recognizes them as
constructors.
Do NOT have a return type (not even void)
Why not?
Since the constructor is called automatically by the
system.
This is the 2nd way the compiler knows they are
constructors.
18
19. Default Constructors
Default constructor
parameter-less constructor, it initializes the variables
to some default value, such as zero
Example
TimeType();
Student();
19
21. Default Constructors
An implicit default (no-argument) constructor is built
into the program automatically by the compiler.
This constructor is used to create the objects when
we don’t write a constructor.
21
22. Initializer List
Counter():count(0)
{
} //constructor
Is equivalent to
Counter()
{
count=0;
}
Initialization takes place following the member function declaration
but before the function body.
It is preceded by a colon
The value is placed in parentheses following the data member.
22
23. Initializer List
Initializer list is also called member-initialization list.
If multiple members are to be initialized, they are
separated by commas.
Example
someClass():m1(5),m2(42),m3(3) { }
Actions complicated than simple initialization must
be carried out in the constructor body.
23
24. Overloaded Constructors
A default constructor initializes data members at default values
such as zero.
Example
Distance() : feet(0),inches(0.0){ } //default constructor
It is convenient to be able to give data members a value (other
than default value) when the objects are created.
Example
Distance dist1(5,6.25);
Defines a Distance object named dist1 and initializes feet
at 5, and inches at 6.25
For this, we need a constructor like this:
Distance (int f, float i) : feet(f),inches(i) { }
24
25. Overloaded Constructors
We have two constructors for Distance
Distance() : feet(0),inches(0.0) { }//default constructor
Distance(int f, float i) : feet(f),inches(i) { }
These two explicit constructors with the same name Distance
makes the constructor overloaded.
Which of these is executed to create an object depends on
how many arguments are used in the object creation.
Distance dist1;//calls default constructor
Distance dist2(11,6.25); //calls constructor with two
arguments
25
26. The Default Copy Constructor
We have seen two variations of constructor.
There is another way of initializing a constructor.
An object can be initialized with another object of the same type.
This type of constructor falls in the category of implicit
constructors.
We don’t need to create a constructor for this.
One is already built into all classes.
It is called the default copy constructor.
It is one argument constructor whose argument is an object of
the same class as the constructor.
26
27. The Default Copy Constructor
Default copy constructor can be invoked in two ways:
Example:
Distance dist1(11, 6.25);
Distance dist2(dist1); //calling default copy constructor
Distance dist3=dist1; //calling default copy constructor
Both the ways have the same results and causes a member-to-
member copy
The second way looks like an assignment statement, but it is not.
Both the formats invoke the default copy constructor.
27
28. Destructors
Another function is called automatically when an object is
destroyed.
Such a function is called a destructor.
Destructor has the name as the constructor (i.e. the class name)
but is preceded by a tilde (~).
The most common use of destructor is to de-allocate memory
that was allocated for the object by the constructor.
28
29. Destructors
~Classname( )
A default do-nothing destructor is provided by the
compiler.
Only one destructor per class
No arguments
No return type
29
30. Templates
C++ supports code reuse in different ways.
The template feature in C++ provides way to reuse
source code.
The concept of Template is applicable to:
Functions
Classes
30
31. Function Templates
Suppose we want to write a function that returns the absolute
value of a number.
Ordinarily, this function would be written for a particular data
type:
int abs(int n) // absolute value of ints
{
return (n<0) ? -n : n; // if n is negative, return –n
}
Here the function is defined to take an argument of type int and
to return a value of this same type.
31
32. Function Templates
But now suppose we want to find the absolute value of a type
long.
We need to write a completely new function:
long abs(long n) // absolute value of longs
{
return (n<0) ? -n : n;
}
And again, for type float:
float abs(float n) // absolute value of floats
{
return (n<0) ? -n : n;
}
32
33. Function Templates
The body of the function is same in each case, but they must be
separate functions because they handle variables of different
types.
It’s true that in C++ these functions can all be overloaded to have
the same name, but we must nevertheless write a separate
definition for each one.
Rewriting the same function body over and over for different
types wastes time as well as space in the listing.
33
34. Function Templates
Also, if we find we’ve made an error in one such function, we’ll
need to remember to correct it in each function body.
Failing to do this correctly is a good way to introduce
inconsistencies into our programs.
It would be nice if there were a way to write such a function just
once and have it work for many different data types.
This is exactly what Function templates do for us.
34
36. Function Templates
The key innovation in function templates is to represent the data
type used by the function not as a specific type such as int, but by a
name that can stand for any type.
In the function template above, this name is T.
The template keyword signals the compiler that we are about to
define a Function template.
The keyword class, within the angle brackets, might just as well be
called type.
As we’ve seen, we can define our own data types using classes, so
there’s really no distinction between types and classes.
The variable following the keyword class (T in this example) is called
the template argument.
36
37. Function Templates
What does the compiler do when it sees the template keyword and
the Function definition that follows it?
The function template itself doesn’t cause the compiler to generate
any code.
It can’t generate code because it doesn’t know yet what data type
the function will be working with.
It simply remembers the template for possible future use.
Code generation doesn’t take place until the function is actually
called (invoked) by a statement within the program.
This happens in expressions such as abs(int1) in the statement
cout << "abs(" << int << ")=" << abs(int1);
37
38. Function Templates
When the compiler sees a function call, it knows that
the type to use is int, because that’s the type of the
argument int1.
So it generates a specific version of the abs()
function for type int, substituting int wherever it sees
the name T in the function template.
This is called instantiating the function template, and
each instantiated version of the function is called a
template function. 38
39. Function Templates
Notice that the amount of RAM used by the program
is the same whether we use the template approach
or write three separate functions.
What we’ve saved is having to type three separate
functions into the source file. This makes the listing
shorter and easier to understand.
Also, if we want to change the way the function
works, we need to make the change in only one
place in the listing instead of three.
39
40. Function Templates with Multiple
Arguments
Let’s look at another example of a function template.
This one takes three arguments: two template
arguments and one basic type.
The purpose of this function is to search an array for
a specific value.
The function returns the array index for that value if
it finds it, or -1 if it can’t find it.
The arguments are a pointer to the array, the value
to search for, and the size of the array.
40
41. Function Templates with Multiple
Arguments
template <class atype>
int find(const atype* array, atype value, int size) { Output
for(int j=0; j<size; j++) 'f' in chrArray: index=2
if(array[ j ]==value) return j; 6 in intArray: index=-1
return -1; 4 in dubArray: index=-1
}
int main() {
char chrArr[ ] = {'a', 'c', 'f', 's', 'u', 'z'}; // array
char ch = 'f'; // value to find
int intArr[ ] = {1, 3, 5, 9, 11, 13};
int in = 6;
double dubArr[ ] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0};
double db = 4.0;
cout << "n 'f' in chrArray: index=" << find(chrArr, ch, 6);
cout << "n 6 in intArray: index=" << find(intArr, in, 6);
cout << "n 4 in dubArray: index=" << find(dubArr, db, 6);
return 0;
} 41
42. Template Arguments Must Match
When a template function is invoked, all instances of the same
template argument must be of the same type.
For example, in find(), if the array is of type int, the value to
search for must also be of type int. We can’t say
int intarray[ ] = {1, 3, 5, 7}; // int array
float f1 = 5.0; // float value
int value = find(intarray, f1, 4); // error
Because the compiler expects all instances of atype to be the
same type.
find(int*, int, int); // It can generate a function
find(int*, float, int); // It can’t generate a function
Because the first and second arguments must be the same type.
42
43. Class Templates
The template concept can be applied to classes as
well as to functions.
Class templates are generally used for data storage
(container) classes.
Stacks and linked lists, are examples of data
storage classes.
43
44. Class Templates
The Stack class below, could store data only of type int.
class Stack
{
int st[10]; // array of ints
int top; // index number of top of stack
public:
Stack(); // constructor
void push(int var); // takes int as argument
int pop(); // returns int value
};
44
45. Class Templates
If we wanted to store data of type long in a stack, we would need
to define a completely new class.
class LongStack
{
long st[10]; // array of longs
int top; // index number of top of stack
public:
LongStack(); // constructor
void push(long var); // takes long as argument
long pop(); // returns long value
};
45
46. Class Templates
//Solution with a class template
template <class Type>
class Stack{
Type st[10]; // stack: array of any type
int top; // number of top of stack
public:
Stack(){top = 0;} // constructor
void push(Type); // put number on stack
Type pop(); // take number off stack
};
46
47. Class Templates
template<class Type>
void Stack<Type>::push(Type var) // put number on stack
{ if(top > 10-1) // if stack full,
cout<< "Stack is full!";
st[top++] = var;
}
template<class Type>
Type Stack<Type>::pop() // take number off stack
{ if(top <= 0) // if stack empty,
cout<< "Stack is empty!";
return st[--top];
}
47
48. Class Templates
int main() {
Stack<float> s1; // s1 is object of class Stack<float>
// push 2 floats, pop 2 floats
s1.push(1111.1);
s1.push(2222.2);
cout << "1: " << s1.pop() << endl;
cout << "2: " << s1.pop() << endl;
Stack<long> s2; // s2 is object of class Stack<long>
// push 2 longs, pop 2 longs
s2.push(123123123L);
s2.push(234234234L);
cout << "1: " << s2.pop() << endl;
cout << "2: " << s2.pop() << endl;
return 0;
} 48