Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructor Spl member fn auto ini of object

Similar presentations


Presentation on theme: "Constructor Spl member fn auto ini of object"— Presentation transcript:

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

2 Characteristics of a Constructor

3 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 };

4 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();

5 Default constructor

6 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; }

7 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();

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

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

10

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

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

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

14 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; }

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

16 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.

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

18 #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); };

19 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; }

20 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;

21 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

22 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;

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

24 Destructor

25 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.

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

27 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();

28 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.

29 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.

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

31 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.

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

33 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.

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

35 Student Book Types of Operator Unary operator Binary operator

36 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

37 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++); };

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

39 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)

40 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); };

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

42 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

43


Download ppt "Constructor Spl member fn auto ini of object"

Similar presentations


Ads by Google