0% found this document useful (0 votes)
36 views1 page

Using Namespace STD Int Main (String STR ("Please Split This")

This document discusses Run Time Type Identification (RTTI) in C++, which allows identifying the data type of variables at runtime using typeid operator and dynamic_cast operator. It explains how to safely cast pointers and references to base types to derived types using dynamic_cast. It also discusses using typeid operator to check if two pointers point to objects of the same type.

Uploaded by

2622799308i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

Using Namespace STD Int Main (String STR ("Please Split This")

This document discusses Run Time Type Identification (RTTI) in C++, which allows identifying the data type of variables at runtime using typeid operator and dynamic_cast operator. It explains how to safely cast pointers and references to base types to derived types using dynamic_cast. It also discusses using typeid operator to check if two pointers point to objects of the same type.

Uploaded by

2622799308i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

RTTI

Identify data type of varies during run time


typeid operator, which returns the type of a given expression
dynamic_cast operator, which safely converts a pointer or reference
to a base type into a pointer or reference to a derived type
Pointer-Type dynamic_casts
if(Derived *dp = dynamic_cast<Derived*>(bp))
{
//use the Derived object to which dp points
} else {
//use the Base object to which bp points
}
Reference-Type dynamic_casts
void f(const Base &b)
{
try {
const Derived &d = dynamic_cast<const Derived&>(b);
} catch (bad_cast) {
}
}
typeid
Derived *dp = new Derived;
Base *bp = dp; // both ptrs point ot a Derived obect
if(typeid(*dp) == typeid(*dp)) {
//if both points to objs of same type
}
if(typeid(*bp) == typeid(Derived)) {
//bp actually pts to Derived
}

String Token
#include<iostream>
#include<cstring> //stcpy,strtok
#include<string>
using namespace std;
int main() {
string str ("Please split this");
char* cstr = new char[str.length()+1];
strcpy(cstr, str.c_str());
char*p = strtok(cstr, " ");
while (p!=0)
{
cout << p << '\n';
p = strtok(NULL, " ");
}
delete[] cstr;
}

class templates
template< typename T>
class LinkedList
{
public:
void add( T x);
void print(void);
};

Generic programing v.s polymorphism


Polymorphism:using different algorithms for different(but related) objects
E.G: draw specific shape objects in different ways
Generic programing:using the same algorithm for different objects

A function template is a generic function that can work


with any data type. The programmer writes the specifications of
the function, but substitutes parameters for data types. When
the compiler encounters a call to the function, it generates code to
handle the specific data type(s) used in the call.
Class templates: templates may also be used to create generic classes
and abstract data types. Class templates allow you to create one general
version of a class without having to duplicate code to handle multiple data types.
polymorphism allows an object reference variable or an object pointer to reference
objs pf different types to call the correct member funcs, depending upon the type of
obj being referenced.
Inheritance allows a new class to be based on an existing class.The new class inherits
all member variables and functions(except the constr,desru) of the class it is based on.
Binary tree:non search binary tree, where each node has up to two leaves
Binary search tree:Speical type of Binary tree that
1)left child node is smaller than its parent Node
2)right child node is greater than its parent Node
Binary Heap: A complete binary tree which satisfies the heap ordering property
1)min-heap property:the value of each node is greater than or equal to the value of its
parent, with min-value element at the root
2)max-heap property:the value of each node is less than or equal to the value of its
parent, with max-value element at the root
Factory function creates the appropriate obj (depends on parameter value and return
to a ptr to the new obj.

Generic swap/sort function


template <typename T>
void swap(T&a, T&b)
{
T temp = a;
a = b;
b = temp;
}

operator expression
Value& operatorr=(const Value &rhs);
Value operator* (const &p, const Value &q)
rewrite c = a*b
c.operator=(operator*(a,b))
overloaded >
bool operator>(const Value& x) const
{
return I > x.i;
}

read/write file

Dynamic memory allocation


int *p = new int[10];
delete p;

Overloaded +
XYpoint& operator+(XYpoint& rhs) {
this->x += rhs.getX();
this->y += rhs.getY();
};

An overloaded function is a function that shares its name with one or more other functions, but which has
a different parameter list. The compiler chooses which function is desired based upon the arguments used.
An overridden function is a method in a descendant class that has a different definition than a virtual function in an ancestor class. The compiler chooses
which function is desired based upon the type of the object being used to call the function.
A redefined function is a method in a descendant class that has a different definition than a non virtual function in an ancestor class.S ince the method is
not virtual, the compiler chooses which function to call based the static type of the obj reference rather than the actual t ype of obj.
this point is a ptr provides the address of the current instance of a class

New Section 1 Page 1

You might also like