Вы находитесь на странице: 1из 21

DAY: 1 CONCEPT DEFINITION

UNIT: II DESCRIPTION

TOPIC: CONSTRUCTOR QUESTIONS

A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the value of data members of the class.

1. Explain the various types of constructors. 2. Explain the various types of constructors with example.

TYPES

Default constructor or no argument constructor Parameterized constructor Copy constructor Dynamic constructor

DEFINITION

Default constructor or no argument constructor Constructor is a special member function It enables an object to initialize itself when it is created It is also known as automatic initialization of objects It constructs the values of data members of a class Constructor name will be same as class name

(Default constructor)

class default_constructor Syntax { public: default_constructor() {------} }; class integer Example { int m,n; public: integer(void) //constructor declared
1

}; Integer :: integer(void) { m=0,n=0; }

void main() { Integer I; }

Parameterized constructor DEFINITION (Parameterized constructor) A constructor which takes the parameter or arguments are called as Parameterized constructor The constructor integer() may be modified to take arguments as below Then a constructor function has been parameterized the object declaration statement such as Integer int1; It will not work. We must pass the initial values as arguments to the constructor function when an object is declared. Integer int1=integer(0,100); Integer int1(0,100); Syntax: class parameterized_constructor { public: parameterized_constructor(int,int)
2

//explicitly //implicitly

By calling the constructor explicitly By calling the constructor implicitly

{ }; Example

#include<iostream.h> class integer { int m,n;

public: integer(int,int); void display(void) { cout<<m=<<m; }; integer::integer(intx,inty) { m=x; n=y; : cout<<n=<<n; }

int main() { integer int1(0,100); //implicit call

integer int2=integer(25,75); cout<<object1<<\n; int1.display(); cout<<object2<<\n; int2.display(); return 0;


3

Output

Object1 Object2 DEFINITION


(Copy

m=0 M=25

n=100 N=75

constructor)

Copy constructor A copy constructor is used to declare and initialize an object from another object integer l2(l1); It would define the object l2 and at the same time initialize it to the values of l1 integer l2=l1; The process of initializing through a copy constructor is known as Copy initialization l2=l1; It will not invoke the copy constructor. If l1 & l2 are objects, it assigns the values of l1 and l2 member by member. A copy constructor takes a reference to an object of the same class as itself as an argument class copy_constructor { public: copy_constructor(copy_constructor &x) { }
4

Syntax

}; Example #include<iostream.h> class code { int id; public: code(int a) //constructor { id=a; } //copy constructor //copy in the value }

code(code &x) { id=x.id;

void display(void) { cout<<id; }; int main() { code A(100); code B(A); code C=A; code D; cout<<\n id of A:; cout<<\n id of B:; cout<<\n id of c:; A.display(); B.display(); c.display();
5

Output

cout<<]n id of D:; return 0; } Id of A 100 100 100

D.display();

DAY: 2

Id of B Id of C

UNIT: II
CONCEPT DEFINITION

TOPIC: DESTRUCTOR

SYNTAX DIFFERENCE

DESCRIPTION A destructor as the name implies is used to destroy the objects that have been created by a constructor. Like a constructor the destructor is a member function whose name is the same as the class name but is preceded by a tilde symbol. ~ integer ( ) { } Constructor It is used to construct the object Destructor It is used to destroy the object

QUESTONS 1. What is destructor? Write down the difference between constructor and destructor.

It can be called automatically when an It is called automatically when an object object comes into existence goes out of scope The constructor function is called every The destructor function is called every time time an object is created the program exits a block It is the function, invoked first before It is the function called at last calling any function

It takes any number of arguments It can be overloaded A class can have multiple constructor

It takes no arguments

It cannot be overloaded Only one destructor function for a class is possible

Constructor name and the class name Destructor name and class name should be should be the same the same but it is prefixed by ~ Eg: Class student { public: Student( );//constructor }; It cannot be declared as virtual function It behaves like a new operator Eg: Class student { public: ~student( ); } It can be a virtual function It behaves like a delete operator

DAY:3 CONCEPT DEFINITION

TOPIC:OPERATOR OVERLOADING DESCRIPTION QUESTIONS The mechanism of giving special meaning to an existing operator is known as operator 1. Define operator overloading. over loading. Explain with an example.
7

UNIT:2

RULES

Rules for overloading an operator Only existing operators can be overloaded. The overloaded operator must have at least one operand that is of user defined data type. The basic meaning of the operator should not be changed. That is, if the operator to be overloaded to is + operator then it should be used only for addition operation, not for other arithmetic or any other purposes. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. Except the function call operator ( ), no other operator can have a default argument Some of the operators cannot be simply overloaded.

2. Explain operator overloading. List down the characteristics of operator overloading 3. List down the rules for overloading an operator 4. Write a program to find the sum of two complex numbers using operator overloading.

Operators that cannot be overloaded. The operators which cannot be overloaded are: The dot operator for member access The dereference member to class operator .* Scope resolution operator:: Size operator sizeof Conditional operator ?: Casting operators static_cast<>, dynamic_cast<>, const_cast<> # and ## tokens for macro preprocessors The syntax for operator function is, Program Return type Operator Operator-symbol(arglist)
8

SYNTAX

reinterpret_cast<>,

Function body

#include<iostream.h> #include<string.h> class complex { float real; float imag; public: complex(float tempreal=0, float tempimag=0); { real=tempreal; imag=tempimag; }

complex add(complex comp1) { float tempreal; float tempimag; tempreal=real+comp2.real; tempiamg=imag+comp2.imag; return complex(tempreal,tempimag); }
9

complex operator+(complex comp2) { float tempreal; float tempimag; tempreal=real+comp2.real; tempiamg=imag+comp2.imag; return complex(tempreal,tempimag); } void display() { cout<<real<<+<<imag<<\n; } }; void main() { complex comp1(10,20); complex comp2(20,30); complex compresult1, compresult2; compresult1=comp1.add(comp2); compresult1.display(); compresult2=comp1+comp2;
10

compresult2.display()
} DAY:4 CONCEPT TYPES

UNIT: II DESCRIPTION There are three types of conversions. Conversion from built-in data type to an object Conversion from object to a built in data type Conversion from one object to another object type

TOPIC: TYPE CONVERSION QUESTIONS 1. Explain in briefly


about Type Conversion with examples

Any conversion involving a class is defined either by a conversion constructoe or by a conversion operator function A conversion operator function performs conversion in the opposite direction , that is it converts an object of the current class to another type. General form: Operator type-name {
CONDITIONS

SYNTAX

Function body

A conversion operator function is a function that satisfies the following conditions It must be a member function It must not specify any return type It must not have any arguments

Conversion from built-in data type to object Conversion from basic data type to object type can be done with the help of
11

constructors. Example:1

Constructors takes a single argument whose type is to be converted.

#include<iostream.h> #include<conio.h> class sample { int x; public: sample() { }

sample(int a) { x=a; }

void print() { }; void main() { sample s1; s1=5; s1.print(); } The statement s1=5; converts built-in data type int to object s1 by invoking the conversion constructor
12

cout<<x;

Conversion from object to built-in datatype Using conversion operator function, conversion from object of a class to a built-in data type can be done. Example:2 #include<iostream.h> #include<conio.h> class sample { int x; public: sample(int a) { x=a; }

operator int() { }; void main() { sample s1(10); int a =s1; cout<<a; } The statement int a=s1; converts the object s1 to built-in data type int by invoking the overloaded operator function. The above conversion operator function can also
13

return x;

be invoked explicitly as int a =(int)s1; Or int a int(s1); The compiler invokes the appropriate conversion function . The compiler looks for an overloaded operator =. Conversion from one object to another object
(

We can convert from one type of an object into another type of object using either constructors or conversion operator function. Example:3 #include<iostream.h> #include<conio.h> class sample1 { private: int x; public: sample1(int a) { x=a; }
(

int geta() { }; class sample2 return x; }

14

private: int y; public: sample2() { }

sample2(sample1 s1) { y=s1.geta(); }

void print() { }; void main() { sample1 s1; sample2 s2; s2=s1; s2.print(); } S1 &s2 are objects of different classes., s1 is object of sample1 class and s2 is the object of sample2 class. S2=s1; Converts the object s1 into s2 by invoking the constructor function
DAY:5 CONCEPT

cout<<y;

UNIT:II DESCRIPTION
15

TOPIC: FRIEND FUNCTION QUESTIONS

1. Explain Operator We can use friends as operator function overloading through friend Friend is a violation of principle of information hiding and it is to be avoided as functions far as possible Member functions cannot choose the first argument and it is always the invoking object There are two cases where it is really important to use friend functions

#include<iostream.h> class matrix { int element[3][3]; public: matrix() }; matrix(int tempmatrix[3][3]) for(inti=0;i<3;i++) for(int j=0;j<3;j++) element[i][j]=tempmatrix[i][j]; } void read() { for(int i=0;i<3;i++) for(int j=0;j<3;j++)
16

cin>>element[i][j]; } void display() { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<element[i][j]<<; } cout<<\n; } } friend matrixoperator*(matrix,int); friend matrixoperator*(int,matrix); }; matrix operator*(matrix tempmatrix, int multiplier) { for(int i=0;i<3;i++) for(int j=0;j<3;j++) tempmatrix.element[i][j]=multiplier* tempmatrix.element[i][j];
17

return matrix(tempmatrix.element); } matrix operator*(int multiplier,matrix tempmatrix) { for(int i=0;i<3;i++) for(int j=0;j<3;j++) tempmatrix.element[i][j]=multiplier* tempmatrix.element[i][j]; return matrix(tempmatrix.element); } void main() { int Array int11[][3]={1,2,3,4,5,6,7,8,9}; int Array int12[][3]={4,5,6,7,8,9,1,2,3}; matrix m1(Array (int1)); matrix m2(Array(int2)); matrix m3,m4; m1.display(); m3=m1*5; m3.display(); m2.display(); m4=5*m2;
18

m4.display(); }
DAY: 6 CONCEPT DEFINITION

UNIT: II DESCRIPTION Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example:

TOPIC: DESTRUCTORS QUESTIONS 1. Explain destructors with an example.

SYNTAX

2. Define destructor. Explain the characteristics of it with examples

class X { public: // Constructor for class X X(); // Destructor for class X ~X(); };

CHARACTERIST ICS

A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. A destructor can be declared virtual or pure virtual. If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor's class type. The following is equivalent to the function the compiler would implicitly define for A:
A::~A() { }

The compiler first implicitly defines the implicitly declared destructors of the base classes and nonstatic data members of a class A before defining the implicitly declared destructor
19

of AA destructor of a class A is trivial if all the following are true:


It is implicitly defined All the direct base classes of A have trivial destructors The classes of all the nonstatic data members of A have trivial destructors

The destructor for a class object is called before destructors for members and bases are called. 1. Destructors for nonstatic members are called before destructors for base classes are called. 2. Destructors for nonvirtual base classes are called before destructors for virtual base classes are called.
#include <string> class Y { private: char * string; int number; public: // Constructor Y(const char*, int); // Destructor ~Y() { delete[] string; } }; // Define class Y constructor Y::Y(const char* n, int a) { string = strcpy(new char[strlen(n) + 1 ], n); number = a; } int main () { // Create and initialize // object of class Y Y yobj = Y("somestring", 10);

PROGRAM:1

20

// ... // Destructor ~Y is called before // control returns from main() }

PROGRAM:2

We can use a destructor explicitly to destroy objects, although this practice is not recommended. However to destroy an object created with the placement new operator, We can explicitly call the object's destructor. The following example demonstrates this:
#include <new> #include <iostream> using namespace std; class A { public: A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; } }; int main () { char* p = new char[sizeof(A)]; A* ap = new (p) A; ap->A::~A(); delete [] p; }

The statement A* ap = new (p) A dynamically creates a new object of type A not in the free store but in the memory allocated by p. The statement delete [] p will delete the storage allocated by p, but the run time will still believe that the object pointed to by ap still exists until you explicitly call the destructor of A (with the statement ap->A::~A()).

21

Вам также может понравиться