Constructor Spl member fn auto ini of object

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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Operator Overloading Fundamentals
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Constructors & Destructors Review CS 308 – Data Structures.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Overloading and Type Conversions
Operator Overloading Customised behaviour of operators Unit - 06.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
OPERATOR OVERLOADING Customised behaviour of operators.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Constructors and Destructors
Constructors and Destructors
Operator Overloading Ritika Sharma.
Learning Objectives Pointers as dada members
Access Functions and Friend Functions
Chapter 13: Overloading and Templates
By Muhammad Waris Zargar
Overloading Operator MySting Example
CONSTRUCTORS & DESTRUCTORS
Introduction Rules Types Programs
Operator Overloading.
Developed By : Ms. K. S. Kotecha
Concepts of Constructors and Its Types
Objectives Define polymorphism Overload functions
Constructor & Destructor
Constructors & Destructors
Inheritance C++ strongly support the reusability, that is one class has been written and tested already it can be adapted or used to create a new class.
Operator Overloading BCA Sem III K.I.R.A.S.
group work #hifiTeam
Lecture Constructors and Destructors
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructor & Destructor
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Operator Overloading; String and Array Objects
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Presentation transcript:

Constructor Spl member fn auto ini of object When obj created auto cons called Constructs values for data members.

Characteristics of a Constructor

Overloading constructors You can have more than one constructor in a class, as long as each has a different list of arguments.   class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

Overloading constructors rectangle::rectangle() { height = 10; width = 10; xpos = 0; ypos = 0; }   void main() rectangle rc1(3.0, 2.0); rectangle rc2(); rc1.draw(); rc2.draw();

Default constructor

Class student { Private: Char name[20]; Int roll no; Char sex; Float weight; Float height; Public: Student(); Void display(); }; Student::student() Name[0]=‘\0’; Rollno=0; Sex=‘\0’; Weight=0; Height=0; }

Void student::display() { Cout<<“name=“<<name<<endl; Cout<<“rollno=“<<rollno<<endl; Cout<<“sex=“<<sex<<endl; Cout<<“weight=“<<weight<<endl; Cout<<“height=“<<height<<endl; } Void main() Student s; Cout<<default constructor; S.display(); Getch();

Default Argument constructor Class const { Private: Int a; Int b; Public: Const(int y=56); Void display(); }; Const::const(int y) b=y; }

Void const::display() { Cout<<a; Cout<<b; } Void main() Const c; C.display(); Getch();

Example class cons { private: int a; public: cons(); cons(int x=56); void display(); }; cons::cons() a=23; } cons::cons(int x) a=x;

void cons::display() { cout<<“a=“<<a<<endl; } void main() clrscr(); cons c; c.display(); getch();

Parametrized constructor If the constructor contains one or more arguments then it is called as parametrized constructor

class cons { private: int a; Int b; public: cons(); cons(int x,int y); void display(); }; cons::cons(int x,int y) a=x; b=y; }

void cons::display() { cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl; } void main() clrscr(); cons c(10,20); c.display(); getch();

Dynamic Constructor(constructor with dynamic allocation) The constructors can also be used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object are not of the same size, thus resulting in the saving of memory. Allocation of memory to object at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of the new operator.

Syntax class sample { private: char *name; public: sample(char *tname) }

#include<iostream.h> #include<string> class string { private: char *name; int length; public: string() length=0; name=new char[length+1]; } string(char *s) length=strlen(s); //<data type><variable-name>=new<datatype>[size] strcpy(name,s); void display(void) cout<<name<<“\n”; void join(string &a,string &b); };

void string::join(string &a,string &b) { length=a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); }; int main() clrscr(); char *first=“anu”; string name1(first),name2(“dhivya”),name3(“indhu”),s1,s2; s1.join(name1,name2); s2.join(s1,name3); name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); getch(); return 0; }

Copy constructor It is used to declare and initialize an object from another object. for example the statement integer I2(I1); would define the object I2 and at the same time initialize it to the values of I1.Another form of this statement is integer I2=I1;

The general syntax Class_name::class_name(class_name&ptr) Symbolic representation X::x(x&ptr) X->user defined class name Ptr->pointer to class object x

class cons { private: int a; int b; public: cons(int x,int y); cons(cons&ptr) void display(); }; cons::cons(int x,int y) a=x; b=y; } cons::cons(cons,&ptr) a=ptr.a; b=ptr.b; void cons::display() cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl;

void main() { clrscr(); cons c1(10,20); cons c2=c1; cons c2(c1); c2.display(); getch(); }

Destructor

Rules It is declared with no return types It cannot be declared static ,const or volatile. It should have public access in the class declaration Use tilde(~) before the destructor name. It takes no arguments and therefore cannot be overloaded.

Syntax class user_name { private: //data variables //methods protected: //data public: user_name();//constructor ~user_name();//destructor };

Example class cons { private: int a; int b; public: cons() a=45; b=47; } ~cons() cout<<“All the allocated memory destroyed”<<endl; getch(); void display() cout<<“a”<<a<<endl; cout<<“b”<<b<<endl; }; void main() clrscr(); cons c; c.display();

Operator Overloading The mechanism of giving special meanings to an operator is known as operator overloading. It provides a flexible option for the creation of new definitions for most of the c++ operators.

Syntax: return_type class_name::operator op(arg_list) { function body } return_type->the type of value returned by the specified operation op->the operator being overloaded. operator->the op is preceded by the keyword operator operator op is the function name.

Operators that Can and Cannot be Overloaded Deitel & Deitel, Figure 22.1 Deitel & Deitel, Figure 22.2 Operator Overloading CS-2303, C-Term 2010

Rules Only existing operators can be overloaded. New operators cannot be created The overloaded operator must have at least one operand that is user-defined type We cannot change the basic meaning of an operator.this is to say we cannot redefine the plus(+)operator to subtract one value from the other Overloaded operators follow the syntax rules of the original operators.they cannot be overridden. There are some operators that cannot be overloaded We cannot use friend function to overload certain operators.however member functions can be used to overload them.

Where a friend cannot be used = Assignment operator ()Function call operator [] Subscripting operator ->class member access operator

Example: Operator Overloading Student Book Example: Operator Overloading class OverloadingExample { private: int m_LocalInt; public: OverloadingExample(int j) // default constructor m_LocalInt = j; } int operator+ (int j) // overloaded + operator return (m_LocalInt + j); }; This example uses the ‘+’ overloaded operator for class level addition operations.

OverloadingExample object1(10); Student Book void main() { OverloadingExample object1(10); cout << object1 + 10; // overloaded operator called }

Student Book Types of Operator Unary operator Binary operator

Student Book Unary Operators Operators attached to a single operand (-a, +a, --a, a--, ++a, a++) The pre and post increment and decrement operators and overloading in different ways. This is because they have a different effect on objects and their values. e.g. a=14; cout << a++; // will print 14 and increment a cout << ++a; // will increment a and print 15

Example: Unary Operators Student Book Example: Unary Operators class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) m_LocalInt = j; } int operator++ () return (m_LocalInt++); };

Example: Unary Operators Student Book Example: Unary Operators void main() { UnaryExample object1(10); cout << object1++; // overloaded operator results in value // 11 }

Student Book Binary Operators Operators attached to two operands (a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b)

Example: Binary Operators Student Book Example: Binary Operators class BinaryExample { private: int m_LocalInt; public: BinaryExample(int j) m_LocalInt = j; } int operator+ (BinaryExample& rhsObj) return (m_LocalInt + rhsObj.m_LocalInt); };

BinaryExample object1(10), object2(20); Student Book void main() { BinaryExample object1(10), object2(20); cout << object1 + object2; // overloaded operator called }

Non-Overloadable Operators Student Book Non-Overloadable Operators Operators that can not be overloaded due to safety reasons: Member Selection ‘.’ operator Member dereference ‘.*’ operator Exponential ‘**’ operator User-defined operators Operator precedence rules Exponential operator is reserved User-defined operators because of precedence problem