Professional Documents
Culture Documents
Rizoan Toufiq1
1 Assistant Professor
Constructor
A classs constructor is called each time an object of that class is created.
Perform any initialization
A constructor function has the same name.
A constructor function has no return type.
Destructor
Called when an object is destroyed.
Application(Example): an object that allocates memory when it is
created will want to free that memory when it is destroyed.
Sytax: The name of destructor is the name of its class, preceded by a
∼.
Cons Destructor.cpp
Cons Destructor.cpp
Cons Destructor.cpp
Object Pointer.cpp
Object Pointer.cpp
Object Pointer.cpp
int main(){
myclass ob (120) ; // create object
myclass *p; // create pointer to object
p = &ob; // put address of ob into p
cout << " Value using object : " << ob.get ();
cout << "\n";
cout << " Value using pointer : " << p->get ();
return 0;
}
The in-line function are not actually called but, rather, are expanded
in line, at the point of each call.
Advantage:
No overhead associated with the function call and return mechanism.
This means that in-line functions can be executed much faster than
normal functions
Disadvantage:
If they are too large and called too often, your program grows larger.
functions.
inlineFunc.cpp
AutoinlineFunc.cpp
AutoinlineFunc.cpp
AssigningObject.cpp
AssigningObject.cpp
1. Error
Only objects of the same type can be used in an assignment statement.
myclass o1;
yourclass o2;
2.
It is important to understand that all data members of one object are
assigned to another when an assignment is performed.
3. Error
Memory allocation and Assigning object
3. Error
3. Error
3. Error
3. Error
When s1 and s2 are created, both allocate memory to hold their re-
spective strings.
When s1 is assigned to s2, s2s p now points to the same memory as
s1s p.
Thus, when these objects are destroyed, the memory pointed to by s2s
p is freed twice and the memory originally pointed to by s2s p is not
freed at all.
1. Call by Value
1. Call by Value
1. Output
2. Call by Reference
2. Call by Reference
2. Output
3. Output
Constructing
Destructing
100
Destructing
Program
Program
return str;
}
Program
// Load a string .
void samp :: set ( char *str ){
s = ( char *) malloc ( strlen (str)+1);
if (!s){
cout << " Allocation error \n";
exit (1) ;
}
strcpy (s, str);
}
Output
A friend is not a member of a class but still has access to its private
elements.
Two reasons that friend functions are useful have to do with operator
overloading and the creation of certain types of I/O functions.
A third reason for friend functions is that there will be times when you
want one function to have access to the private members of two
or more different classes.
ExampleCode
ExampleCode
ExampleCode
class truck{
int weight ;
int speed ;
public :
truck (int w, int s) { weight = w; speed = s; }
friend int sp_greater ( car c, truck t);
};
// Initialize an array .
# include <iostream >
using namespace std;
class samp {
int a;
public :
samp (int n) { a = n; }
int get_a () { return a; }
};
this Pointer
this Pointer
public :
inventory ( char *i, double c, int o){
strcpy (this ->item , i); // access members
this -> cost = c; // through the this
this -> on_hand = o; // pointer
}
void show ();
};
this Pointer
Syntax:
*p = 100;
cout << " Here is integer at p: " << *p << "\n";
delete p; // release memory
return 0;
}
int i;
for (i=0; i <5; i++)
p[i] = i;
for (i=0; i <5; i++){
cout << " Here is integer at p[" << i << "]: ";
cout << p[i] << "\n";
}
delete [] p;
return 0;
}
Herbert Schildt,
Teach Yourself C++
- Chapter 2,3 and 4
Walter Savitch
Problem Solving with C++
Herbert Schildt
Teach Yourself C++
Y. Daniel Liang
Introduction to Java programming
Paul Deitel
Java How to program
Herbert Schildt
Java: the complete reference