Dynamic object creation

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CS Advanced C++ Exception Handling Topic #5.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 15: Operator Overloading
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Operator overloading Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Overloading and Type Conversions
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Learners Support Publications Classes and Objects.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Operator Overloading Ritika Sharma.
Pointers and Dynamic Arrays
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
Object Oriented Programming COP3330 / CGS5409
Operator Overloading
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Classes and Objects.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Dynamic object creation

Object creation When a C++ object is created, two events occur: Storage is allocated for the object. The constructor is called to initialize that storage. Step one can occur in several ways Storage can be allocated before the program begins, in the static storage area. This storage exists for the life of the program. Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace). Storage can be allocated from a pool of memory called the heap (also known as the free store)

Operator new C++ can combine all the actions necessary to create an object into a single operator called new. When you create an object with new (using a new-expression), it allocates enough storage on the heap to hold the object and calls the constructor for that storage. MyType *fp = new MyType(1,2); Here at runtime the equivalent of malloc(sizeof(MyType) i)s called (often, it is literally a call to malloc( )), and the constructor for MyType is called with the resulting address as the this pointer, using (1,2) as the argument list The default new checks to make sure the memory allocation was successful before passing the address to the constructor. If the constructor has no arguments, you can write the new-expression without the constructor argument list: MyType *fp = new MyType;

Operator delete The complement to the new-expression is the delete-expression it first calls the destructor and then releases the memory (often with a call to free( )). Just as a new-expression returns a pointer to the object, a delete-expression requires the address of an object. delete fp; delete can be called only for an object created by new

Memory manager overhead When you create automatic objects on the stack, the compiler knows the exact type, quantity, and scope. Creating objects on the heap involves additional overhead, both in time and in space. You call malloc( ), which requests a block of memory from the pool The block must be recorded so further calls to malloc( ) won’t use it, and so that when you call free( ), the system knows how much memory to release.

new & delete for arrays In C++, we can create arrays of objects on the stack or on the heap with equal ease, and the constructor is called for each object in the array. Ex: MyType* fp = new MyType[100]; This allocates enough storage on the heap for 100 MyType objects and calls the constructor for each one. For deleting the array we use the syntax delete []fp; The empty brackets tell the compiler to generate code that fetches the number of objects in the array and calls the destructor for that many array objects.

Running out of storage when the operator new cannot find a contiguous block of storage large enough to hold the desired object, a special function called the new-handler is called. The default behavior for the new-handler is to throw an exception if you’re using heap allocation in your program, it’s wise to replace the new-handler with a message that says you’ve run out of memory and then aborts the program. The behavior of the new-handler is tied to operator new so if you overload operator new the new-handler will not be called by default. If you want to call the new-handler, you have to write the code to do so inside your overloaded operator new

Example #include <iostream> #include <cstdlib> #include <new> using namespace std; int count = 0; void out_of_memory() { cerr << "memory exhausted after " << count << " allocations!" << endl; exit(1); } int main() { set_new_handler(out_of_memory); while(1) { count++; new int[1000]; // Exhausts memory

Overloading new & delete When we create a new-expression first, storage is allocated using the operator new and then the constructor is called. In a delete-expression, the destructor is called and then storage is de allocated using the operator delete. The constructor and destructor calls are never under our control, but we can change the storage allocation functions operator new and operator delete. C++ allows you to overload new and delete to implement your own storage allocation scheme

Overloading new & delete The most common reason to change the allocator is efficiency Another issue is heap fragmentation. By allocating objects of different sizes it’s possible to break up the heap so that you effectively run out of storage. When you overload operator new and operator delete, it’s important to remember that you’re changing only the way raw storage is allocated. The compiler will simply call your new instead of the default version to allocate storage, then call the constructor for that storage.

When you overload operator new, you also replace the behavior when it runs out of memory so you must decide what to do in your operator new: return zero, write a loop to call the newhandler and retry allocation, or (typically) throw a bad_alloc exception

Overloading global new & delete when the global versions of new and delete are unsatisfactory for the whole system, you can overload the global versions That is make the defaults completely inaccessible –you can’t even call them from inside your redefinitions. The overloaded new must take an argument of size_t (the Standard C standard type for sizes). This argument is generated and passed to you by the compiler and is the size of the object you’re allocating. You must return a pointer either to an object of that size or to zero if you can’t find the memory (in which case the constructor is not called!).

Example public: #include <cstdio> S() { puts("S::S()"); } }; int main() { puts("creating & destroying an int"); int* p = new int(47); delete p; puts("creating & destroying an s"); S* s = new S; delete s; puts("creating & destroying S[3]"); S* sa = new S[3]; delete []sa; } #include <cstdio> #include <cstdlib> using namespace std; void* operator new(size_t sz) { printf("operator new: %d Bytes\n", sz); void* m = malloc(sz); if(!m) puts("out of memory"); return m; } void operator delete(void* m) { puts("operator delete"); free(m); class S { int i[100];

Overloading new & delete for a class the syntax is the same as overloading any other operator. When the compiler sees you use new to create an object of your class, it chooses the member operator new over the global version. However, the global versions of new and delete are used for all other types of objects You can also overload new & delete operators for arrays

Operator overloading

Operator overloading The mechanism of giving special meaning to an operator is known as operator overloading Although the semantics of the operator can be extended, we cannot change its syntax, the grammatical rule like the number of operands, precedence , associativity etc. When an operator is overloaded its meaning is not lost We cannot overload Class member access operators (. , .*) Scope resolution operator(::) Size operator (sizeof) Conditional operator(?:)

Operator overloading is done with the help of operator function return type classname:: operator op(op-arglist) { function body//TASK DEFINED } Operator functions must be either member functions or friend functions The process of overloading includes the steps Create the class that defines the data type that is to be used in the overloading operation Declare the operator function operator op() in the public part of the class. It may be either friend or member function Define the operator function to implement the required operations

basic difference between friend function and member function is Operator overloading basic difference between friend function and member function is member function will not have any argument for unary operators while friend function will have one argument Member function will have one argument for binary operators while friend function will have two arguments vector operator +(vector); //vector addition vector operator – (); // unary minus friend vector operator +(vector, vector); // vector addition friend vector operator – (vector); //unary minus

operator overloading overloading unary minus class space { int x,y,z; public : void getdata(int a, int b, int c); void display(void); void operator – ( ); }; void space::getdata(int a, int b, int c) { x=a; y=b; z=c; } void space :: display(void) {cout<<x<<“ “<<y<<“ “ <<z;} void space::operator-() { x= -x; Y=-y; Z=-z; } int main() { space S; S.getdata(10, -20, 30); cout<<“S : ” ; S.display(); -S; cout<<“S :”; Return 0;

overloaded functions are invoked by expressions such as Operator overloading overloaded functions are invoked by expressions such as op x or x op for unary operators x op y for binary operators

operator overloading- overloading binary operator class complex { float x,y; public: complex(){} complex(float real, float imag) {x=real; y=imag;} complex operator +(complex); void display(); }; complex complex :: operator +(complex c) {complex temp; temp.x = x+c.x; temp.y =y+c.y; return temp; } void complex :: display() { cout<<x<<"+j"<<y; } int main complex C1,C2,C3; C1 = complex(2.5, 3.5); C2 = complex(1.6 ,2.7); C3 = C1+C2; cout<<"C1=";C1.display(); cout<<"C2=";C2.display(); cout<<"C3=";C3.display(); return 0;

Operator overloading in binary operator overloading, the left –hand operand is used to invoke the operator function and the right hand operand is passed as an argument

Overloading binary operators using friends void complex :: display() { cout<<x<<"+j"<<y; } int main complex C1,C2,C3; C1 = complex(2.5, 3.5); C2 = complex(1.6 ,2.7); C3 = C1+C2; cout<<"C1=";C1.display(); cout<<"C2=";C2.display(); cout<<"C3=";C3.display(); return 0; class complex { float x,y; public: complex(){} complex(float real, float imag) {x=real; y=imag;} friend complex operator+ (complex, complex); void display(); }; complex operator +(complex a, complex b) {complex temp; temp.x = a.x+b.x; temp.y =a.y+b.y; return temp; }

Rules for overloading operators Only existing operators can be overloaded The overloaded operator must have atleast one operand that is of user defined type We cannot change the basic meaning of an operator Overloaded operators follow the syntax rules of the original operators There are some operators that cannot be overloaded We cannot use friend functions to overload certain operators When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class

Where a friend cannot be used = assignment operator () function call operator [] subscripting operator -> class member access operator

Manipulation of strings using operators Strings can be defined as class objects which can be then manipulated like the built-in types Since the strings vary greatly in size, we use new to allocate memory for each string and a pointer variable to point to the string array A typical string class will look like this Class string { Char *p; Int len; Public: //member functions to initialize //and manipulate strings };

Mathematical operations on strings #include<iostream.h>’ #include<string.h> Class string { Char *p; Int len; Public: String(){len=0;p=0;} String(const char * s); String(const string & s);// copy constructor ~string(){delete p;} //+ operator friend int operator+(const string &s, const string &t); //<= operator friend int operator<=(const string &s, const string &t); Friend void show(const string s); };

String :: string(const char *s) { Len=strlen(s); P=new char[len+1]; Strcpy(p,s); } String :: string(const string & s) Len=s.len; Strcpy(p,s.p); String operator+(const string &s,const string &t) String temp; Temp.len=s.len+t.len; Temp.p=new char[temp.len+1]; Strcpy(temp.p,s.p); Strcat(temp.p,t.p); Return(temp);

Int operator<=(const string &s, const string &t) { Int m=strlen(s.p); Int n=strlen(t.p); If(m<=n) return(1); Else return(0); } Void show(const string s) Cout<<s.p;

Int main() { String s1=“new”; String s2=“york” String s3=“delhi”; string t1=s1; string t2=s2; string t3=s1+s3; Show(t1); Show(t2); Show(t3); If(t1<=t3) Cout<<“smaller than”; } Else Return 0;

Type conversions Consider the following statement v3=v1+v2;// v1 & v2 are objects of different classes For built in types the compiler will do type conversions But for user defined types, we must design the conversion routines Three types of situations might arise in the data conversion between uncompatible types Conversion from basic type to class type Conversion from class type to basic type Conversion from one class type to another class type

Basic to class type Here we can use constructors to perform a type conversion from argument’s type to constructor’s class type String :: string(const char *s) { Len=strlen(s); P=new char[len+1]; Strcpy(p,s); } The constructor builds a string type object from a char* type variable a

Example: string s1, s2; Char* name1=“ibm pc”; Char* name2=“apple”; S1=string(name1); S2=name2; The constructors used for the type conversion take a single argument whose type is to be converted

Class to basic type C++ allows us to define an overloaded casting operator that could be used to convert a class type data to a basic type The general form of an overloaded casting operator function usually referred to as conversion function, is: Operator typename() { //(function statements) } This function converts a class type to typename

vector :: operator double() { Double sum=0; For(int i=0;i<size;i++) Ex: vector :: operator double() { Double sum=0; For(int i=0;i<size;i++) sum=sum+v[i]*u[i]; Return sqrt(sum); } the operator double() can be used as follows: Double length=double(v1); or Double length=v1; Where v1 is an object of type vector The casting operator function should satisfy the following conditions It must be a class member It must not specify any return type It must not have any arguments

One class to another class type Example : Objx=objy; //object of different types Objx belongs to class x Objy belongs to class y Y->source class X->destination class The conversion can be carried out by either a constructor or a conversion function It depends upon whether the type conversion function to be located in the source class or destination class

It can be invoked by d1=s1; or d1=invet2(s1); Ex1: //inside class invent1 Operator invent2() { Invent2 temp; Temp.code=code; Temp.value=price*items; Return temp; } Ex2: //inside invent2 Invent2(invent1 p) Code=p.getcode(); Value=p.getitems()*p.getprice;

Conversion required Conversion takes place in Source class Destination class Basic->class Not applicable constructor Class->basic Casting operator Class->class

References & Copy-Constructor

Pointers in C++ The most important difference between pointers in C and those in C++ is that C++ is a more strongly typed language. C doesn’t let you casually assign a pointer of one type to another, but it does allow you to accomplish this through a void*. Thus, bird* b; rock* r; void* v; v = r; b = v; This feature of C allows you to quietly treat any type like any other type. C++ doesn’t allow this

References in C++ A reference (&) is like a constant pointer that is automatically dereferenced. It is usually used for function argument lists and function return values. EX: #include <iostream> int y; int& r = y; const int& q = 12; // (1) int x = 0; // (2) int& a = x; // (3) int main() { cout << "x = " << x << ", a = " << a << endl; a++; }

There are certain rules when using references A reference must be initialized when it is created. (Pointers can be initialized at any time.) Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.) You cannot have NULL references. You must always be able to assume that a reference is connected to a piece of storage.

References in functions The most common place you’ll see references is as function arguments and return values. When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function. you could do the same thing by passing a pointer, but a reference has much cleaner syntax

#include<iostream.h> int* f(int* x) { (*x)++; return x; // Safe, x is outside this scope } int& g(int& x) { x++; // Same effect as in f() return x; // Safe, outside this scope int main() { int a = 0; f(&a); // Ugly (but explicit) g(a); // Clean (but hidden)

Const references Making the argument a const reference will allow the function to be used in all situations. This means that, for built-in types, the function will not modify the argument, and for user-defined types, the function will call only const member functions The use of const references in function arguments is especially important because your function may receive a temporary object. This might have been created as a return value of another function or explicitly by the user of your function. Temporary objects are always const, so if you don’t use a const reference, that argument won’t be accepted by the compiler.

EX: void f(int&) {} void g(const int&) {} int main() { //! f(1); // Error g(1); }

The copy-constructor A copy constructor is used to declare and initialize an object from another object A copy constructor takes a reference to an object of the same class as itself as an argument The syntax is: Class-name (class-name &i){ } We cannot pass the argument by value to a copy constructor

Example of copy constructor void display() { cout<<id; } }; void main() code a(100); code b(a); //copy constructor code c=a; code d; d=a; a.display(); b.display(); c.display(); d.display(); #include<iostream.h> class code { int id; public: code(){ } code(int a) id=a; } code(code & x) //copy constructor id=x.id;

Pointers to members A pointer is a variable that holds the address of some location. You can change what a pointer selects at runtime, and the destination of the pointer can be either data or a function. The C++ pointer-to-member follows this same concept, except that what it selects is a location inside a class EX: struct Simple { int a; }; int main() { Simple so, *sp = &so; sp->a; so.a; }

Operator overloading

Operator overloading The mechanism of giving special meaning to an operator is known as operator overloading Although the semantics of the operator can be extended, we cannot change its syntax, the grammatical rule like the number of operands, precedence , associativity etc. When an operator is overloaded its meaning is not lost We cannot overload Class member access operators (. , .*) Scope resolution operator(::) Size operator (sizeof) Conditional operator(?:)

Operator overloading is done with the help of operator function return type classname:: operator op(op-arglist) { function body//TASK DEFINED } Operator functions must be either member functions or friend functions The process of overloading includes the steps Create the class that defines the data type that is to be used in the overloading operation Declare the operator function operator op() in the public part of the class. It may be either friend or member function Define the operator function to implement the required operations

basic difference between friend function and member function is Operator overloading basic difference between friend function and member function is member function will not have any argument for unary operators while friend function will have one argument Member function will have one argument for binary operators while friend function will have two arguments vector operator +(vector); //vector addition vector operator – (); // unary minus friend vector operator +(vector, vector); // vector addition friend vector operator – (vector); //unary minus

operator overloading overloading unary minus class space { int x,y,z; public : void getdata(int a, int b, int c); void display(void); void operator – ( ); }; void space::getdata(int a, int b, int c) { x=a; y=b; z=c; } void space :: display(void) {cout<<x<<“ “<<y<<“ “ <<z;} void space::operator-() { x= -x; Y=-y; Z=-z; } int main() { space S; S.getdata(10, -20, 30); cout<<“S : ” ; S.display(); -S; cout<<“S :”; Return 0;

overloaded functions are invoked by expressions such as Operator overloading overloaded functions are invoked by expressions such as op x or x op for unary operators x op y for binary operators

operator overloading- overloading binary operator class complex { float x,y; public: complex(){} complex(float real, float imag) {x=real; y=imag;} complex operator +(complex); void display(); }; complex complex :: operator +(complex c) {complex temp; temp.x = x+c.x; temp.y =y+c.y; return temp; } void complex :: display() { cout<<x<<"+j"<<y; } int main complex C1,C2,C3; C1 = complex(2.5, 3.5); C2 = complex(1.6 ,2.7); C3 = C1+C2; cout<<"C1=";C1.display(); cout<<"C2=";C2.display(); cout<<"C3=";C3.display(); return 0;

Operator overloading in binary operator overloading, the left –hand operand is used to invoke the operator function and the right hand operand is passed as an argument

Overloading binary operators using friends void complex :: display() { cout<<x<<"+j"<<y; } int main complex C1,C2,C3; C1 = complex(2.5, 3.5); C2 = complex(1.6 ,2.7); C3 = C1+C2; cout<<"C1=";C1.display(); cout<<"C2=";C2.display(); cout<<"C3=";C3.display(); return 0; class complex { float x,y; public: complex(){} complex(float real, float imag) {x=real; y=imag;} friend complex operator+ (complex, complex); void display(); }; complex operator +(complex a, complex b) {complex temp; temp.x = a.x+b.x; temp.y =a.y+b.y; return temp; }

Rules for overloading operators Only existing operators can be overloaded The overloaded operator must have atleast one operand that is of user defined type We cannot change the basic meaning of an operator Overloaded operators follow the syntax rules of the original operators There are some operators that cannot be overloaded We cannot use friend functions to overload certain operators When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class

Where a friend cannot be used = assignment operator () function call operator [] subscripting operator -> class member access operator

Manipulation of strings using operators Strings can be defined as class objects which can be then manipulated like the built-in types Since the strings vary greatly in size, we use new to allocate memory for each string and a pointer variable to point to the string array A typical string class will look like this Class string { Char *p; Int len; Public: //member functions to initialize //and manipulate strings };

Mathematical operations on strings #include<iostream.h>’ #include<string.h> Class string { Char *p; Int len; Public: String(){len=0;p=0;} String(const char * s); String(const string & s);// copy constructor ~string(){delete p;} //+ operator friend int operator+(const string &s, const string &t); //<= operator friend int operator<=(const string &s, const string &t); Friend void show(const string s); };

String :: string(const char *s) { Len=strlen(s); P=new char[len+1]; Strcpy(p,s); } String :: string(const string & s) Len=s.len; Strcpy(p,s.p); String operator+(const string &s,const string &t) String temp; Temp.len=s.len+t.len; Temp.p=new char[temp.len+1]; Strcpy(temp.p,s.p); Strcat(temp.p,t.p); Return(temp);

Int operator<=(const string &s, const string &t) { Int m=strlen(s.p); Int n=strlen(t.p); If(m<=n) return(1); Else return(0); } Void show(const string s) Cout<<s.p;

Int main() { String s1=“new”; String s2=“york” String s3=“delhi”; string t1=s1; string t2=s2; string t3=s1+s3; Show(t1); Show(t2); Show(t3); If(t1<=t3) Cout<<“smaller than”; } Else Return 0;

Type conversions Consider the following statement v3=v1+v2;// v1 & v2 are objects of different classes For built in types the compiler will do type conversions But for user defined types, we must design the conversion routines Three types of situations might arise in the data conversion between uncompatible types Conversion from basic type to class type Conversion from class type to basic type Conversion from one class type to another class type

Basic to class type Here we can use constructors to perform a type conversion from argument’s type to constructor’s class type String :: string(const char *s) { Len=strlen(s); P=new char[len+1]; Strcpy(p,s); } The constructor builds a string type object from a char* type variable a

Example: string s1, s2; Char* name1=“ibm pc”; Char* name2=“apple”; S1=string(name1); S2=name2; The constructors used for the type conversion take a single argument whose type is to be converted

Class to basic type C++ allows us to define an overloaded casting operator that could be used to convert a class type data to a basic type The general form of an overloaded casting operator function usually referred to as conversion function, is: Operator typename() { //(function statements) } This function converts a class type to typename

vector :: operator double() { Double sum=0; For(int i=0;i<size;i++) Ex: vector :: operator double() { Double sum=0; For(int i=0;i<size;i++) sum=sum+v[i]*u[i]; Return sqrt(sum); } the operator double() can be used as follows: Double length=double(v1); or Double length=v1; Where v1 is an object of type vector The casting operator function should satisfy the following conditions It must be a class member It must not specify any return type It must not have any arguments

One class to another class type Example : Objx=objy; //object of different types Objx belongs to class x Objy belongs to class y Y->source class X->destination class The conversion can be carried out by either a constructor or a conversion function It depends upon whether the type conversion function to be located in the source class or destination class

It can be invoked by d1=s1; or d1=invet2(s1); Ex1: //inside class invent1 Operator invent2() { Invent2 temp; Temp.code=code; Temp.value=price*items; Return temp; } Ex2: //inside invent2 Invent2(invent1 p) Code=p.getcode(); Value=p.getitems()*p.getprice;

Conversion required Conversion takes place in Source class Destination class Basic->class Not applicable constructor Class->basic Casting operator Class->class

References & Copy-Constructor

Pointers in C++ The most important difference between pointers in C and those in C++ is that C++ is a more strongly typed language. C doesn’t let you casually assign a pointer of one type to another, but it does allow you to accomplish this through a void*. Thus, bird* b; rock* r; void* v; v = r; b = v; This feature of C allows you to quietly treat any type like any other type. C++ doesn’t allow this

References in C++ A reference (&) is like a constant pointer that is automatically dereferenced. It is usually used for function argument lists and function return values. EX: #include <iostream> int y; int& r = y; const int& q = 12; // (1) int x = 0; // (2) int& a = x; // (3) int main() { cout << "x = " << x << ", a = " << a << endl; a++; }

There are certain rules when using references A reference must be initialized when it is created. (Pointers can be initialized at any time.) Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.) You cannot have NULL references. You must always be able to assume that a reference is connected to a piece of storage.

References in functions The most common place you’ll see references is as function arguments and return values. When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function. you could do the same thing by passing a pointer, but a reference has much cleaner syntax

#include<iostream.h> int* f(int* x) { (*x)++; return x; // Safe, x is outside this scope } int& g(int& x) { x++; // Same effect as in f() return x; // Safe, outside this scope int main() { int a = 0; f(&a); // Ugly (but explicit) g(a); // Clean (but hidden)

Const references Making the argument a const reference will allow the function to be used in all situations. This means that, for built-in types, the function will not modify the argument, and for user-defined types, the function will call only const member functions The use of const references in function arguments is especially important because your function may receive a temporary object. This might have been created as a return value of another function or explicitly by the user of your function. Temporary objects are always const, so if you don’t use a const reference, that argument won’t be accepted by the compiler.

EX: void f(int&) {} void g(const int&) {} int main() { //! f(1); // Error g(1); }

The copy-constructor A copy constructor is used to declare and initialize an object from another object A copy constructor takes a reference to an object of the same class as itself as an argument The syntax is: Class-name (class-name &i){ } We cannot pass the argument by value to a copy constructor

Example of copy constructor void display() { cout<<id; } }; void main() code a(100); code b(a); //copy constructor code c=a; code d; d=a; a.display(); b.display(); c.display(); d.display(); #include<iostream.h> class code { int id; public: code(){ } code(int a) id=a; } code(code & x) //copy constructor id=x.id;

Pointers to members A pointer is a variable that holds the address of some location. You can change what a pointer selects at runtime, and the destination of the pointer can be either data or a function. The C++ pointer-to-member follows this same concept, except that what it selects is a location inside a class EX: struct Simple { int a; }; int main() { Simple so, *sp = &so; sp->a; so.a; }