Object Oriented Design and Programming,
Unit-02, SRM University, II Semester, Regulation 2021
Size: 384.06 KB
Language: en
Added: May 29, 2024
Slides: 40 pages
Slide Content
21CSC101T OBJECT ORIENTED DESIGN AND PROGRAMMING Dr.M.Sivakumar AP/NWC SRMIST
Course Outcomes (CO ) At the end of this course, learners will be able to: CO-1: Create programs using object-oriented approach and design methodologies CO-2: Construct programs using method overloading and operator overloading CO-3: Create programs using inline, friend and virtual functions, construct programs using standard templates CO-4: Construct programs using exceptional handling and collections CO-5: Create Models of the system using UML Diagrams
Unit-2 - Methods and Polymorphism Constructors - Types of constructors Static constructor and Copy constructor Destructor Polymorphism : Constructor overloading Method Overloading Operator Overloading UML Interaction Diagrams Sequence Diagram Collaboration Diagram - Example Diagram
Constructors A constructor is a special member function of a class that is automatically called when an instance of the class is created.
Constructors It is very common for some part of an object to require initialization before it can be used. Suppose you are working on 100's of objects and the default value of a particular data member is needed to be zero. Initializing all objects manually will be very tedious job. Instead, you can define a constructor function which initializes that data member to zero. Then all you have to do is declare object and constructor will initialize object automatically
Constructors While defining a constructor you must remember that the name of constructor will be same as the name of the class constructors will never have a return type Syntax: class MyClass { public: // Constructor declaration MyClass () { // Constructor body // Initialization code goes here } };
Constructors Constructors are automatically called when an object of the class is created. For example: int main() { MyClass obj1; // Calls the default constructor MyClass obj2(10); // Calls the parameterized constructor return 0; }
Defining Constructors Inside the class Outside the class
Defining Constructors – Inside the class #include< iostream > using namespace std; class student { int rno ; char name[50]; double fee; public: student()// Explicit Default constructor { cout <<"Enter the RollNo :"; cin >> rno ; cout <<"Enter the Name:"; cin >>name; cout <<"Enter the Fee:"; cin >>fee; } void display() { cout << endl << rno <<"\t"<<name<<"\t"<<fee; } }; int main() { student s; s.display (); return 0; }
Defining Constructors – Outside the class // defining the constructor outside the class #include < iostream > using namespace std; class student { int rno ; char name[50]; double fee; public: student(); void display() { cout << endl << rno << "\t" << name << "\t" << fee; } }; student::student() { cout << "Enter the RollNo :"; cin >> rno ; cout << "Enter the Name:"; cin >> name; cout << "Enter the Fee:"; cin >> fee; } int main() { student s; s.display (); return 0; }
Types of constructors Default Constructor Parameterized Constructor Copy Constructor
Default Constructor As soon as the object is created the constructor is called which initializes its data members. A default constructor is so important for initialization of object members, that even if we do not define a constructor explicitly, the compiler will provide a default constructor implicitly.
Parameterized Constructor These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument.
Copy Constructor These are special type of Constructors which takes an object as argument Is used to copy values of data members of one object into other object. It is usually of the form X (X&) , where X is the class name. The compiler provides a default Copy Constructor to all the classes.
Copy Constructor As it is used to create an object, hence it is called a constructor. And, it creates a new object, which is exact copy of the existing copy, hence it is called copy constructor .
Destructor A destructor works opposite to constructor it destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically. A destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde sign (~).
Destructor #include < iostream > using namespace std; class Employee { public : Employee() { cout <<"Constructor Invoked"<< endl ; } ~Employee() { cout <<"Destructor Invoked"<< endl ; } }; int main( void ) { Employee e1; //creating an object of Employee Employee e2; //creating an object of Employee return 0; } Output: Constructor Invoked Constructor Invoked Destructor Invoked Destructor Invoked
Polymorphism
Polymorphism: Constructor overloading When we overload a constructor more than a purpose it is called constructor overloading.
Method Overloading #include < iostream > using namespace std; class Cal { public: int add( int a,int b) { return a + b; } int add( int a, int b, int c) { return a + b + c; } }; int main(void) { Cal C; //class object declaration. cout << C.add (10, 20)<< endl ; cout << C.add (12, 20, 23); return 0; }
Operator Overloading the ability to provide the operators with a special meaning for a data type a compile-time polymorphism we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +
Operators that can be Overloaded in C++ Unary operators Binary operators Special operators ( [ ], (), etc)
Operators that can be Overloaded in C++ Operators that can be overloaded Examples Binary Arithmetic +, -, *, /, % Unary Arithmetic +, -, ++, — Assignment =, +=,*=, /=,-=, %= Bitwise & , | , << , >> , ~ , ^ De-referencing (->) Dynamic memory allocation, De-allocation New, delete Subscript [ ] Function call () Logical &, | |, ! Relational >, < , = =, <=, >=
Operator that cannot be overloaded Scope operator (::) Sizeof member selector(.) member pointer selector(*) ternary operator(?:)
Rules for Operator Overloading Existing operators can only be overloaded, but the new operators cannot be overloaded. The overloaded operator contains atleast one operand of the user-defined data type. We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators. When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument. When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.
Syntax of Operator Overloading return_type class_name : : operator op( argument_list ) { // body of the function. } return type - the type of value returned by the function class_name - the name of the class operator op - an operator function where op is the operator being overloaded, and the operator is the keyword
Overloading Unary operator #include < iostream > using namespace std; class OperOverLoad { private: int num; public: OperOverLoad ( int n) { num=n; } void operator ++() { num = num+2; } void display() { cout <<"The Count is: "<<num; } }; int main() { OperOverLoad obj (7); ++ obj ; // calling of a function "void operator ++()" obj.display (); return 0; }
Overloading Binary operator #include < iostream > using namespace std; class Complex { private: int real, imag ; public: Complex( int r = 0, int i = 0) // with default arguments { real = r; imag = i ; } Complex operator+(Complex obj ) { Complex res; res.real = real + obj.real ; res.imag = imag + obj.imag ; return res; } void print() { cout << real << " + i " << imag << '\n'; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); } Output: 12 + i9
UML Interaction Diagrams Interaction diagrams are used to observe the dynamic behavior of a system . Interaction diagram visualizes the communication and sequence of message passing in the system . Interaction diagram represents the structural aspects of various objects in the system . Interaction diagram represents the ordered sequence of interactions within a system . Interaction diagram provides the means of visualizing the real time data via UML .
UML Interaction Diagrams This interactive behavior is represented in UML by two diagrams known as Sequence diagram Collaboration diagram. Sequence diagram emphasizes on time sequence of messages from one object to another. Collaboration diagram emphasizes on the structural organization of the objects that send and receive messages .
How to Draw an Interaction Diagram ? Purpose of interaction diagrams is to capture the dynamic aspect of a system. So to capture the dynamic aspect, we need to understand what a dynamic aspect is and how it is visualized. Dynamic aspect can be defined as the snapshot of the running system at a particular moment. Following things are to be identified clearly before drawing the interaction diagram Objects taking part in the interaction. Message flows among the objects. The sequence in which the messages are flowing. Object organization .
Sequence Diagram A sequence diagram is a type of interaction diagram because it describes how—and in what order—a group of objects works together. A sequence diagram simply depicts interaction between objects in a sequential order i.e . the order in which these interactions take place . The sequence diagram captures the time sequence of the message flow from one object to another
Sequence Diagram for course registration
Sequence Diagram for ATM
Sequence Diagram for Online Shopping
Collaboration Diagram T he collaboration diagram describes the organization of objects in a system taking part in the message flow Communication diagrams also illustrate interactions between objects or components, but they focus more on the relationships and interactions between objects rather than the sequence of messages.
Collaboration diagram for ATM
Collaboration diagram for Online Shopping
Learning Resources Grady Booch , Robert A. Maksimchuk , Michael W. Engle, Object-Oriented Analysis and Design with Applications , 3rd ed., Addison-Wesley, May 2007 Reema Thareja , Object Oriented Programming with C++, 1st ed., Oxford University Press, 2015 Sourav Sahay , Object Oriented Programming with C++, 2nd ed., Oxford University Press, 2017 Robert Lafore , Object-Oriented Programming in C++, 4th ed., SAMS Publishing, 2008 Ali Bahrami , Object Oriented Systems Development”, McGraw Hill, 2004