CONSTRUCTORS AND DESTRUCTORS

Size: px
Start display at page:

Download "CONSTRUCTORS AND DESTRUCTORS"

Transcription

1 UNIT-II CONSTRUCTORS AND DESTRUCTORS Contents: Constructors Default constructors Parameterized constructors Constructor with dynamic allocation Copy constructor Destructors Operator overloading Overloading through friend function Overloading the assignment operator Type conversion Explicit constructor Constructors: What is the use of Constructor? The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. Constructor: Constructor is a special member function. This is used to give initial values to member variables. The general form is: Rules: constructor_name(parameters) statements to give initial values to member variables It should be declared as public. No return type needed. When the object is declared for the class, it automatically executes the constructor and initialized the variables. There are two types of constructors. They are: 1. Constructor without parameters 2. Constructor with parameters (or) parameterized constructor

2 1. Constructor without parameters OR Default Constructor: This constructor has no arguments in it. Default Constructor is also called as no argument constructor. For example: class Exforsys private: int a,b; public: Exforsys();... ; Exforsys :: Exforsys() a=0; b=0; 2. Constructor with parameters OR Parameterized constructor: Constructor can be invoked with arguments. The arguments list can be specified within braces similar to arguments-list in the function constructors with arguments are called parameterized constructors. We must pass the initial values as arguments to the constructor function when object is declared. The general form is: Classname object( argument_list ); (or) Classname object = classname(argument_value_list);

3 This can be done in two ways: 1. By calling the constructor explicitly. 2. By calling the constructor implicitly. For Example: class sample int a,b; public: sample(int x,int y); ; sample::sample(int x,int y) a=x; b=y; Example: smaple S=sample(2,3);//explicit call; sample S(2,3);//implicit call; //parameterized constructors #include<iostream.h> class sample int a,b; public: sample(int,int);//constructor decalred void display()

4 count<< a= <<a<<end/; count<< b= <<b<<end/; ; sample::sample(int x,int y) a=x; b=y; main() sample S(2,3);//implicit call sample S2=sample(4,5);//explicit call S1.display(); S2.display(); Output: a a =2 b = 3 a = 4 b = 5 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 for each object when the objects are not of the same size, thus resulting in the saving of memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. //Constructors with new #include<iostream.h>

5 class vector int * v; // pointer to vector int sz; // Size of a vector public: vector(int size) sz=size; v=new int[size]; ~vector() delete v; //release vector memory void read() for(int i=0;i<sz;i++) cin>>v[i]; void show_sum() int sum=0; for(int i=0;i<sz;i++) Sum+=v[i]; cout<<sum; ; void main() int count; cout<< How many elements are in the vector ; cin>>count;

6 vector v1(count); v1.read(); v1.show_sum(); Copy constructor: This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. For example to invoke a copy constructor the programmer writes: Exforsys e3(e2); or Exforsys e3=e2; Both the above formats can be sued to invoke a copy constructor. For Example: #include <iostream.h> class Exforsys() private: int a; public: Exforsys() Exforsys(int w) a=w;

7 Exforsys(Exforsys& e) a=e.a; cout<< Example of Copy Constructor ; void result() cout<< a; ; void main() Exforsys e1(50); Exforsys e3(e1); cout<< \ne3= ;e3.result(); the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is Example of Copy Constructor: e3=50 Some important points about constructors: A constructor takes the same name as the class name. The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile. No return type is specified for a constructor. The constructor must be defined in the public. The constructor must be a public member.

8 Overloading of constructors is possible. This will be explained in later sections of this tutorial. Multiple Constructor (or) Constructor Overloading: If a class has more than one constructor, then it is called multiple constructors. This technique is called overloading constructors, because all constructors have the same name and they differ only in the number of arguments or data types. Example: #include<iostream.h> #include<conio.h> class con int c; public: con(); con(int x); con(int x,int y); con(int x,int y,int z); ; con ::con() c=0; cout<<"\n\n\t Result is :\t"<<c; con ::con(int x) c=x*x; cout<<"\n\n\t Result is :\t"<<c; con::con(int x,int y) c=x*y; cout<<"\n\n\t Result is :\t"<<c;

9 con ::con(int x,int y,int z) c=x*y*z; cout<<"\n\n\t Result is :\t"<<c; void main() clrscr(); cout<<"\n\n\t OUTPUT:\n"; con c; con s(3); con s1(3,5); con s2(3,2,2); getch(); Constructor with default arguments: If we give initial values to the member variables inside the argument list of constructor then it is called constructor with default arguments. The general form is: constructor_name (return_type variable1= value1, return_type variable2= value2,. return_type variable n= value n member_variable1 = variable1; member_variable2 = variable2.. member_variable n = variable n;

10 At the time of object declaration, depending upon the value, the constructor automatically assign the values to member variables. If any value is missing this will be taken from the argument list values. Example: #include <iostream> #include <iostream.h> #include <conio.h> class myclass int a,b; public: myclass(int x, int y); // constructor ~myclass(); // destructor void show(); ; myclass::myclass(int x = 30, int y = 40) cout << "In constructor\n"; a = x; b = y; void myclass::show() cout << "A =" << a << endl << "B =" << b << endl; cout << "Sum is " << a+b << endl; void main() clrscr(); myclass ob1(10,20); myclass ob2(5); ob1.show(); ob2.show();

11 Output: A = 10 B = 20 Sum is 30 A = 5 B = 40 Sum is 45 Destructors: 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: class X public: // Constructor for class X X(); // Destructor for class X ~X(); ; 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.

12 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. Suppose a class A has an implicitly declared destructor. 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 of A A 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 If any of the above are false, then the destructor is nontrivial. A union member cannot be of a class type that has a nontrivial destructor. Class members that are class types can have their own destructors. Both base and derived classes can have destructors, although destructors are not inherited. If a base class A or a member of A has a destructor, and a class derived from A does not declare a destructor, a default destructor is generated. The default destructor calls the destructors of the base class and members of the derived class. The destructors of base classes and members are called in the reverse order of the completion of their constructor:

13 1. The destructor for a class object is called before destructors for members and bases are called. 2. Destructors for nonstatic members are called before destructors for base classes are called. 3. Destructors for nonvirtual base classes are called before destructors for virtual base classes are called. When an exception is thrown for a class object with a destructor, the destructor for the temporary object thrown is not called until control passes out of the catch block. Destructors are implicitly called when an automatic object (a local object that has been declared auto or register, or not declared as static or extern) or temporary object passes out of scope. They are implicitly called at program termination for constructed external and static objects. Destructors are invoked when you use the delete operator for objects created with the new operator. For example: #include <string> class Y private: char * string; int number; public: // Constructor Y(const char*, int); // Destructor ~Y() delete[] string; ;

14 // 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); //... // Destructor ~Y is called before // control returns from main() Operator Overloading: An operator function defines the operations that the overloaded operator will perform relative to the class upon which it will work. An operator function is created using the keyword operator. The operator overloading feature of C++ is one of the methods of realizing polymorphism. C++ has the ability to provide the operators with a special meaning to an operator is known as operator overloading. We can overload all the C++ operators except the following: Scope resolution operator(::) Size of operator(size of) Conditional operator(?:) Class member access operator(.,.*, *) Pointer to member declarator(::*)

15 Broadly classifying operators are of two types namely: Unary Operators Binary Operators Unary Operators: As the name implies takes operate on only one operand. Some unary operators are namely ++ called as Increment operator, -- called as Decrement Operator,!, ~, unary minus. Binary Operators: The arithmetic operators, comparison operators, and arithmetic assignment operators all this which we have seen in previous section of operators come under this category. Both the above classification of operators can be overloaded. So let us see in detail each of this. Operator Overloading Unary operators As said before operator overloading helps the programmer to define a new functionality for the existing operator. This is done by using the keyword operator. The general syntax for defining an operator overloading is as follows: return_type classname :: operator operator symbol(argument).. statements;

16 Thus the above clearly specifies that operator overloading is defined as a member function by making use of the keyword operator. In the above: return_type is the data type returned by the function class name - is the name of the class operator is the keyword operator symbol is the symbol of the operator which is being overloaded or defined for new functionality :: - is the scope resolution operator which is used to use the function definition outside the class. The usage of this is clearly defined in our earlier section of How to define class members. For example : Suppose we have a class say Exforsys and if the programmer wants to define a operator overloading for unary operator say ++, the function is defined as Inside the class Exforsys the data type that is returned by the overloaded operator is defined as

17 class Exforsys private:. public: void operator ++( ); ; So the important steps involved in defining an operator overloading in case of unary operators are namely: Inside the class the operator overloaded member function is defined with the return data type as member function or a friend function. The concept of friend function we will define in later sections. If in this case of unary operator overloading if the function is a member function then the number of arguments taken by the operator member function is none as seen in the below example. In case if the function defined for the operator overloading is a friend function which we will discuss in later section then it takes one argument. The operator overloading is defined as member function outside the class using the scope resolution operator with the keyword operator as explained above Now let us see how to use this overloaded operator member function in the program #include <iostream.h> class Exforsys private: int x; public: Exforsys( ) x=0; void display(); //Constructor

18 ; void Exforsys ++( ); void Exforsys :: display() cout<< \nvalue of x is: << x; void Exforsys :: operator ++( ) //Operator Overloading for operator ++ defined ++x; void main( ) Exforsys e1,e2; //Object e1 and e2 created cout<< Before Increment cout << \nobject e1: <<e1.display(); cout << \nobject e2: <<e2.display(); ++e1; //Operator overloading applied ++e2; cout<< \n After Increment cout << \nobject e1: <<e1.display(); cout << \nobject e2: <<e2.display(); The output of the above program is: Before Increment Object e1: Value of x is: 0 Object e1: Value of x is: 0 Before Increment

19 Object e1: Value of x is: 1 Object e1: Value of x is: 1 In the above example we have created 2 objects e1 and e2 f class Exforsys. The operator ++ is overloaded and the function is defined outside the class Exforsys. Operator Overloading Binary Operators : Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading, as with unary operator overloading, can be member function or friend function. The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments. Binary operator overloading, as in unary operator overloading, is performed using a keyword operator. Binary operator overloading example: #include <iostream.h> class Exforsys private: int x; int y; public: Exforsys() //Constructor x=0; y=0;

20 void getvalue( ) //Member Function for Inputting Values cout << \n Enter value for x: ; cin >> x; cout << \n Enter value for y: ; cin>> y; void displayvalue( ) //Member Function for Outputting Values cout << value of x is: << x << ; value of y is: <<y Exforsys operator +(Exforsys); ; Exforsys Exforsys :: operator + (Exforsys e2) //Binary operator overloading for + operator defined int x1 = x+ e2.x; int y1 = y+e2.y; return Exforsys(x1,y1); void main( ) Exforsys e1,e2,e3; //Objects e1, e2, e3 created cout<<\n Enter value for Object e1: ; e1.getvalue( ); cout<<\n Enter value for Object e2: ; e2.getvalue( ); e3= e1+ e2; //Binary Overloaded operator used

21 cout<< \nvalue of e1 is: <<e1.displayvalue(); cout<< \nvalue of e2 is: <<e2.displayvalue(); cout<< \nvalue of e3 is: <<e3.displayvalue(); The output of the above program is: Enter value for Object e1: Enter value for x: 10 Enter value for y: 20 Enter value for Object e2: Enter value for x: 30 Enter value for y: 40 Value of e1 is: value of x is: 10; value of y is: 20 Value of e2 is: value of x is: 30; value of y is: 40 Value of e3 is: value of x is: 40; value of y is: 60 In the above example, the class Exforsys has created three objects e1, e2, e3. The values are entered for objects e1 and e2. The binary operator overloading for the operator + is declared as a member function inside the class Exforsys. The definition is performed outside the class Exforsys by using the scope resolution operator and the keyword operator. The important aspect is the statement: e3= e1 + e2; The binary overloaded operator + is used. In this statement, the argument on the left side of the operator +, e1, is the object of the class Exforsys in which the binary overloaded operator + is a member function. The right side of the operator + is e2. This is passed as an argument to the operator +. Since the object e2 is passed as argument to the operator + inside the function defined for binary operator overloading, the values are accessed as e2.x and e2.y. This is added with e1.x and e1.y, which are accessed

22 directly as x and y. The return value is of type class Exforsys as defined by the above example. There are important things to consider in operator overloading with C++ programming language. Operator overloading adds new functionality to its existing operators. The programmer must add proper comments concerning the new functionality of the overloaded operator. The program will be efficient and readable only if operator overloading is used only when necessary. Operator Overloading Using a Friend Function: The only difference between a friend function and member function is that, the friend function requires the arguments to be explicitly passed to the function and processes them explicitly, whereas the member function considers the first argument implicitly. Friend function can either be used with unary function is as follows: Friend return_type operator operator_symbol(arg1[,arg2]) // body of operator friend function Friend function is not allowed to access members of a class directly, but it can access all the members including the private members by using objects of that class. //unary operator overloading using friend function #include<iostream.h> class complex float real,imag; public:

23 complex() real=imag=0.0; void read data() cout<< Real part? ; cin>>real; cout<< Imag part? ; cin>>imag; void Putdata() cout<< ( <<real; cout<<, <<imag<< ) ; //overloading unary operator overloading friend complex operator-(complex C1) complex C; C.real=-C1.real; C.imag=-C1.imag; return(c); ; void main() complex C1,C2; cout<< Enter Complex C1 //<<end/;

24 C1.readdata(); C2=-C1; C2.Putdata(); Output: Enter Complex C1 Rear Part? 1.5 Imag Part? 1.5 (-1.5,2.5) //Addition of two complex number using friend function #include<iostream.h> class complex float real,imag; public: complex() complex(int realpart) real=realpart; void readata() cout<< Real Part? ; cin>>real; cout<< Imag Part? ; cin>>imag; void putdata() cout<< ( <<real;

25 ; cout<<, <<imag<< ) ; friend complex operator+(complex C1,complex C2) complex operator+(complex C1,complex C2) complex C; C.real=C1.real+C2.real; C.imag=C1.imag+C2.imag; return(c); void main() Complex C1,C2,C3=3.0; Cout<< Enter Complex C1 <<end/; C1.readdata(); Cout<< Enter Complex C2 <<end/; C2.readdata(); C3=C1+C2; C3.putdata(); C3=C1+2.0; C3.putdata(); C3=3.0+C2; C3.putdata(); Output: Enter Complex C1 Real Part? 1 Imag Part? 2 Enter Complex C2

26 Real Part? 3 Imag Part? 4 (4,6) (3,2) (6,4) Using a Friend to Overload ++ or : If we want to use a friend function to overload the increment or decrement operators, you must pass the operand as a reference parameter. This is because friend functions do not have this pointers. #include <iostream> using namespace std; class loc int longitude, latitude; public: loc() loc(int lg, int lt) longitude = lg; latitude = lt; void show() cout << longitude << " "; cout << latitude << "\n"; loc operator=(loc op2); friend loc operator++(loc &op); friend loc operator--(loc &op); ;

27 // Overload assignment for loc. loc loc::operator=(loc op2) longitude = op2.longitude; latitude = op2.latitude; return *this; // i.e., return object that generated call // Now a friend; use a reference parameter. loc operator++(loc &op) op.longitude++; op.latitude++; return op; // Make op-- a friend; use reference. loc operator--(loc &op) op.longitude--; op.latitude--; return op; int main() loc ob1(10, 20), ob2; ob1.show(); ++ob1; ob1.show(); // displays ob2 = ++ob1; ob2.show(); // displays 12 22

28 --ob2; ob2.show(); // displays return 0; If we want to overload the postfix versions of the increment and decrement operators using a friend, simply specify a second, dummy integer parameter. For example, this shows the prototype for the friend, postfix version of the increment operator relative to loc. // friend, postfix version of ++ friend loc operator++(loc &op, int x); Overloading new and delete: It is possible to overload new and delete. You might choose to do this if you want to use some special allocation method. For example, you may want allocation routines that automatically begin using a disk file as virtual memory when the heap has been exhausted. Whatever the reason, it is a very simple matter to overload these operators. The skeletons for the functions that overload new and delete are shown here: // Allocate an object. void *operator new(size_t size) /* Perform allocation. Throw bad_alloc on failure. Constructor called automatically. */ return pointer_to_memory; // Delete an object. void operator delete(void *p)

29 /* Free memory pointed to by p. Destructor called automatically. */ The type size_t is a defined type capable of containing the largest single piece of memory that can be allocated. (size_t is essentially an unsigned integer.) The parameter size will contain the number of bytes needed to hold the object being allocated. This is the amount of memory that our version of new must allocate. The overloaded new function must return a pointer to the memory that it allocates, or throw a bad_alloc exception if an allocation error occurs. Beyond these constraints, the overloaded new function can do anything else you require. When you allocate an object using new (whether your own version or not), the object's constructor is automatically called. The delete function receives a pointer to the region of memory to be freed. It then releases the previously allocated memory back to the system. When an object is deleted, its destructor function is automatically called. Example #include <iostream> #include <cstdlib> #include <new> using namespace std; class loc int longitude, latitude; public: loc() loc(int lg, int lt) longitude = lg; latitude = lt;

30 void show() cout << longitude << " "; cout << latitude << "\n"; void *operator new(size_t size); void operator delete(void *p); ; // new overloaded relative to loc. void *loc::operator new(size_t size) void *p; cout << "In overloaded new.\n"; p = malloc(size); if(!p) bad_alloc ba; throw ba; return p; // delete overloaded relative to loc. void loc::operator delete(void *p) cout << "In overloaded delete.\n"; free(p); int main() loc *p1, *p2;

31 try p1 = new loc (10, 20); catch (bad_alloc xa) cout << "Allocation error for p1.\n"; return 1; try p2 = new loc (-10, -20); catch (bad_alloc xa) cout << "Allocation error for p2.\n"; return 1;; p1->show(); p2->show(); delete p1; delete p2; return 0; Output from this program is. In overloaded new. In overloaded new In overloaded delete. In overloaded delete.

32 Overloading the Assignment Operator: The compiler generates a default assignment operator (operator=) for all classes that do not define their own The default assignment behavior is the same as that of memberwise initialization Memberwise assignment can cause the same memory leak / dangling pointers problems that we saw with memberwise initialization To override this default assignment, the class must define the assignment operator as follows: ClassName& ClassName :: operator=(const ClassName& C)... The above definition will cause the user-defined assignment operator to be used in place of the system-defined default. Example: #include <iostream.h> #include <string.h> class Product public: Product() : name(null) Product(char* str); const Product& operator=(const Product& ); void Print() cout << "Product: " << name << endl; const char* getproduct() const return name; private: char * name; ; Product :: Product(char* str) name = new char[strlen(str)+1]; strcpy(name, str);

33 const Product& Product :: operator=(const Product& p) delete [] name; name = new char[strlen(p.getproduct())+1]; strcpy(name, p.getproduct()); return *this; // This will allow assignments to be chained int main() Product p1("hammer"), p2("shovel"), p3; p3 = p2; // p2 and p3 will have equivalent name strings p2.print(); p3.print(); p3 = p2 = p1; // p1, p2, and p3 will have equivalent name strings p1.print(); p2.print(); p3.print(); return 0; The keyword this : The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself. One of its uses can be to check if a parameter passed to a member function is the object itself. For example, // this #include <iostream> using namespace std; class CDummy public: int isitme (CDummy& param); ; int CDummy::isitme (CDummy& param)

34 if (&param == this) return true; else return false; int main () CDummy a; CDummy* b = &a; if ( b->isitme(a) ) cout << "yes, &a is b"; return 0; Output: yes, &a is b It is also frequently used in operator= member functions that return objects by reference (avoiding the use of temporary objects). Following with the vector's examples seen before we could have written an operator= function similar to this one: CVector& CVector::operator= (const CVector& param) x=param.x; y=param.y; return *this; In fact this function is very similar to the code that the compiler generates implicitly for this class if we do not include an operator= member function to copy objects of this class.

35 Type Conversion: What is Type Conversion It is the process of converting one type into another. In other words converting an expression of a given type into another is called type casting. How to achieve this?: There are two ways of achieving the type conversion namely: 1. Automatic Conversion otherwise called as Implicit Conversion Type casting otherwise called as Explicit Conversion Let us see each of these in detail: Automatic Conversion otherwise called as Implicit Conversion: This is not done by any conversions or operators. In other words value gets automatically converted to the specific type in which it is assigned. Let us see this with an example: #include <iostream.h> void main() short x=6000; int y; y=x; In the above example the data type short namely variable x is converted to int and is assigned to the integer variable y.

36 So as above it is possible to convert short to int, int to float and so on. Type casting otherwise called as Explicit Conversion: Explicit conversion can be done using type cast operator and the general syntax for doing this is datatype (expression); Here in the above datatype is the type which the programmer wants the expression to gets changed as In C++ the type casting can be done in either of the two ways mentioned below namely: C-style casting C++-style casting The C-style casting takes the synatx as (type) expression This can also be used in C++. Apart from the above the other form of type casting that can be used specifically in C++ programming language namely C++-style casting is as below namely: type (expression) This approach was adopted since it provided more clarity to the C++ programmers rather than the C-style casting. Say for instance the as per C-style casting (type) firstvariable * secondvariable

37 is not clear but when a programmer uses the C++ style casting it is much more clearer as below type (firstvariable) * secondvariable Let us see the concept of type casting in C++ with a small example: #include <iostream.h> void main() int a; float b,c; cout<< Enter the value of a: ; cin>>a; cout<< n Enter the value of b: ; cin>>b; c = float(a)+b; cout<< n The value of c is: <<c; The output of the above program is Enter the value of a: 10 Enter the value of b: 12.5 The value of c is: 22.5 In the above program a is declared as integer and b and c are declared as float. In the type conversion statement namely c = float(a)+b; The variable a of type integer is converted into float type and so the value 10 is converted as 10.0 and then is added with the float variable b with value 12.5 giving a resultant float variable c with value as 22.5

38 SUMMARY: Constructor is a special member function. This is used to give initial values to member variables. 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. The operator overloading feature of C++ is one of the methods of realizing polymorphism. Broadly classifying operators are of two types namely: Unary Operators Binary Operators The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself. Type conversion is the process of converting one type into another. In other words converting an expression of a given type into another is called type casting. IMPORTANT QUESTIONS: PART A 1. Define constructor. 2. Define default constructor. 3. Define parameterized constructor. 4. Define default argument constructor. 5. What is the ambiguity between default constructor and default argument constructor? 6. Define copy constructor. 7. Define dynamic constructor. 8. Define const object. 9. Define destructor.

39 10. Define multiple constructors. 11. Write some special characteristics of constructor 12. Which operators can not be overloaded? 13. What is meant by operator overloading? PART B 1. Explain about Data Handling and member function. Explain copy constructor and destructor with suitable C++ coding 2. Explain operator overloading with an example. 3. How do you overload the operators new and delete? Give a sample program to implement both. 4. Discuss about the constructor overloading in detail. Give an example to implement it. 5. Discuss about various types of constructors in detail. Give some examples.

CS35 - Object Oriented Programming

CS35 - Object Oriented Programming Syllabus CS 35 OBJECT-ORIENTED PROGRAMMING 3 0 0 3 (Common to CSE & IT) Aim: To understand the concepts of object-oriented programming and master OOP using C++. UNIT I 9 Object oriented programming concepts

More information

UNIT III- INHERITANCE AND POLYMORPHISM

UNIT III- INHERITANCE AND POLYMORPHISM UNIT III- INHERITANCE AND POLYMORPHISM Objectives: To introduce Inheritance in C++ and to explain its importance. To make understand the different types of inheritance. To define typing conversion and

More information

Module Operator Overloading and Type Conversion. Table of Contents

Module Operator Overloading and Type Conversion. Table of Contents 1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading

More information

C++ 8. Constructors and Destructors

C++ 8. Constructors and Destructors 8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of

More information

Overloading operators

Overloading operators Overloading functions Overloading operators In C++ we can have several functions in one program with the same name. The compiler decides which function has to be called depending on the number and type

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Unit IV Contents. ECS-039 Object Oriented Systems & C++

Unit IV Contents. ECS-039 Object Oriented Systems & C++ ECS-039 Object Oriented Systems & C++ Unit IV Contents 1 Principles or object oriented programming... 3 1.1 The Object-Oriented Approach... 3 1.2 Characteristics of Object-Oriented Languages... 3 2 C++

More information

UNIT POLYMORPHISM

UNIT POLYMORPHISM UNIT 3 ---- POLYMORPHISM CONTENTS 3.1. ADT Conversions 3.2. Overloading 3.3. Overloading Operators 3.4. Unary Operator Overloading 3.5. Binary Operator Overloading 3.6. Function Selection 3.7. Pointer

More information

CS6456 OBJECT ORIENTED PROGRAMMING L T P C

CS6456 OBJECT ORIENTED PROGRAMMING L T P C CS6456 OBJECT ORIENTED PROGRAMMING L T P C 3 0 0 3 OBJECTIVES: To get a clear understanding of object-oriented concepts. To understand object oriented programming through C++. UNIT I OVERVIEW 9 Why Object-Oriented

More information

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of ECE INTERNAL ASSESSMENT TEST 2 Date : 03/10/2017 Marks: 40 Subject & Code : Object Oriented Programming

More information

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p. Preface to the Second Edition p. iii Preface to the First Edition p. vi Brief Contents p. ix Introduction to C++ p. 1 A Review of Structures p. 1 The Need for Structures p. 1 Creating a New Data Type Using

More information

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2 UNIT-2 Syllabus for Unit-2 Introduction, Need of operator overloading, overloading the assignment, binary and unary operators, overloading using friends, rules for operator overloading, type conversions

More information

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED. Constructor and Destructor Member Functions Constructor: - Constructor function gets invoked automatically when an object of a class is constructed (declared). Destructor:- A destructor is a automatically

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members }; Constructors A Member function with the same name as its classis called Constructor and it is used to initilize the objects of that class type with a legal value. A Constructor is a special member function

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them. 1. Why do you think C++ was not named ++C? C++ is a super set of language C. All the basic features of C are used in C++ in their original form C++ can be described as C+ some additional features. Therefore,

More information

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES Polymorphism: It allows a single name/operator to be associated with different operations depending on the type of data passed to it. An operation may exhibit different behaviors in different instances.

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; } . CONSTRUCTOR If the name of the member function of a class and the name of class are same, then the member function is called constructor. Constructors are used to initialize the object of that class

More information

Constructor - example

Constructor - example Constructors 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 the class name. The constructor is invoked whenever

More information

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object CHAPTER 1 Introduction to Computers and Programming 1 1.1 Why Program? 1 1.2 Computer Systems: Hardware and Software 2 1.3 Programs and Programming Languages 8 1.4 What is a Program Made of? 14 1.5 Input,

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

CS304 Object Oriented Programming

CS304 Object Oriented Programming 1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

C++_ MARKS 40 MIN

C++_ MARKS 40 MIN C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

Overloading Operators in C++

Overloading Operators in C++ Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

cout<< \n Enter values for a and b... ; cin>>a>>b;

cout<< \n Enter values for a and b... ; cin>>a>>b; CHAPTER 8 CONSTRUCTORS AND DESTRUCTORS 8.1 Introduction When an instance of a class comes into scope, a special function called the constructor gets executed. The constructor function initializes the class

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8 Chapter 8 Introduction C++ Memory Map Free Stores Declaration and Initialization of pointers Dynamic allocation operators Pointers and Arrays Pointers and Const Pointers and Function Pointer and Structures

More information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions. Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Reaccredited at the 'A' Grade Level by the NAAC and ISO 9001:2008 Certified CRISL rated 'A'

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Pointers. Reference operator (&) ted = &andy;

Pointers. Reference operator (&)  ted = &andy; Pointers We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory,

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan Institute of Engineering & Technology for Diploma Studies 1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value

More information

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in themodel answer scheme. 2) The model answer and the answer written by candidate may

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Padasalai.Net s Model Question Paper

Padasalai.Net s Model Question Paper Padasalai.Net s Model Question Paper STD: XII VOLUME - 2 MARKS: 150 SUB: COMPUTER SCIENCE TIME: 3 HRS PART I Choose the correct answer: 75 X 1 = 75 1. Which of the following is an object oriented programming

More information

Chapter 13: Copy Control. Overview. Overview. Overview

Chapter 13: Copy Control. Overview. Overview. Overview Chapter 13: Copy Control Overview The Copy Constructor The Assignment Operator The Destructor A Message-Handling Example Managing Pointer Members Each type, whether a built-in or class type, defines the

More information

Downloaded from

Downloaded from Unit I Chapter -1 PROGRAMMING IN C++ Review: C++ covered in C++ Q1. What are the limitations of Procedural Programming? Ans. Limitation of Procedural Programming Paradigm 1. Emphasis on algorithm rather

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

For Teacher's Use Only Q No Total Q No Q No

For Teacher's Use Only Q No Total Q No Q No Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9

More information

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Pointers. Developed By Ms. K.M.Sanghavi

Pointers. Developed By Ms. K.M.Sanghavi Pointers Developed By Ms. K.M.Sanghavi Memory Management : Dynamic Pointers Linked List Example Smart Pointers Auto Pointer Unique Pointer Shared Pointer Weak Pointer Memory Management In order to create

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

DELHI PUBLIC SCHOOL TAPI

DELHI PUBLIC SCHOOL TAPI Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Course Title: Object Oriented Programming Full Marks: 60 20 20 Course No: CSC161 Pass Marks: 24 8 8 Nature of Course: Theory Lab Credit Hrs: 3 Semester: II Course Description:

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Object oriented programming

Object oriented programming Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................

More information

A <Basic> C++ Course

A <Basic> C++ Course A C++ Course 5 Constructors / destructors operator overloading Julien Deantoni adapted from Jean-Paul Rigault courses This Week A little reminder Constructor / destructor Operator overloading Programmation

More information

More Tutorial on C++:

More Tutorial on C++: More Tutorial on C++: OBJECT POINTERS Accessing members of an object by using the dot operator. class D { int j; void set_j(int n); int mul(); ; D ob; ob.set_j(4); cout

More information