Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Dynamic Polymorphism Lecture-11. Method Polymorphism In a monomorphic language there is always a one to one relationship between a function name and it’s.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
C++ Classes CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Review (3) Structs, Classes, Data Abstraction.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Chapter 9 Classes: A Deeper Look, Part I Part II.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
1 Introduction to Object Oriented Programming Chapter 10.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real- world.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Object-Oriented Programming
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
group work #hifiTeam
Lecture Constructors and Destructors
C++ Classes & Object Oriented Programming
Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Starting to think about objects...
Constructors and Destructors
Introduction to Classes and Objects
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Types of Computer Languages
Presentation transcript:

Week 02 Object Oriented Analysis and Designing

Constructors Constructors are member functions of any class, which are invoked the moment an instance of the class which they belong is created. They are special functions that have the same name as their class. They are also be overloaded. Need for constructors: An object of any class has a copy of its data members, which require initialization before they are used. 2

Type of Constructors Default Constructor-: – A constructor that accepts no parameters is known as default constructor. If no constructor is defined then the compiler supplies a default constructor. Class:: Circle() { radius = 0.0;} Parameterized Constructor -: – A constructor that receives arguments/parameters, is called parameterized constructor. Circle::Circle(int r) { radius = r;}

class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“\n”; } The first constructor is called The second constructor is called Since radius is a private class data member 4

Destructors – Special member function – Same name as class Preceded with tilde ( ~ ) – No arguments – No return value – Cannot be overloaded – Before system reclaims object’s memory Reuse memory for new objects Mainly used to de-allocate dynamic memory locations 5

Another class Example This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor 6

Time::Time() { hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; } void Time::setTime(int h,int m,int s) { *hour = h; *minute = m; *second = s; } Dynamic locations should be allocated to pointers first 7

void Time::printTime() { cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; } Time::~Time() { delete hour; delete minute;delete second; } void main() { Time *t; t= new Time(3,55,54); t->printTime(); t->setHour(7); t->setMinute(17); t->setSecond(43); t->printTime(); delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de-allocate memory locations When executed, the destructor is called 8

#include class Test{ public: Test(){cout<<"Constructor Invoked "<<endl;} ~Test(){cout<<"Destructor Invoked "<<endl;} }; Test obj1; int main() {cout<<"main() BEGINS"<<endl; Test obj2; { cout<<"INNER BLOCK BEGINS"<<endl; Test obj3; cout<<"INNER BLOCK ENDS"<<endl; } cout<<"main() ENDs"<<endl; return 0; } Life Cycle of an object 9

1.The object, obj1, has global scope, hence its constructor is executed before the execution of main() begins. 2.The object 2 has function scope, hence its constructor is executed after main() beings. 3.The object3 has block scope hence its constructor is executed after the inner block begins. 4.The destructor of object3 is invoked when the inner block ends. 5.The destructor if object 2 is invoked when main() ends 6.The destructor if object1 is invoked when the program ends Output: 10

Operator Polymorphism (operator overloading). It provides new meaning for existing operators to enable them to work with the user defined types. Overload operators are generally inherited by derived class. By overloading operators in this way we can give all the classes in a system a common interface, allowing use to perform similar operators on a range of different object. 11

Overload + operator Int I,j,k; Float p,q,r; K=i+j; R=p+q; Note: + operator is different in different data type. 12

// derived classes #include using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }

Math_grade English_grade Math_grade English_grade Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Student Grade Student A Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Student B 14

#include class StudentGrade{ private: int maths_grade; int english_grade; public: StudentGrade(); void setMathsGrade(int grade_in); void setEnglishGrade(int grade_in); int getMathsGrade(); int getEnglishGrade(); StudentGrade operator+(StudentGrade a); }; 15

StudentGrade::StudentGrade() { maths_grade=0; english_grade=0; } void StudentGrade::setMathsGrade(int grade_in){ maths_grade=grade_in; } void StudentGrade::setEnglishGrade(int grade_in){ english_grade=grade_in; } int StudentGrade::getMathsGrade(){ return maths_grade; } int StudentGrade::getEnglishGrade(){ return english_grade; } StudentGrade StudentGrade::operator+(StudentGrade a){ int temp_maths=maths_grade + a.maths_grade; int temp_english=english_grade + a.english_grade; StudentGrade temp_student; temp_student.setMathsGrade(temp_maths); temp_student.setEnglishGrade(temp_english); return temp_student; } 16

int main(){ StudentGrade studentA,studentB,grade_Total; studentA.setMathsGrade(50); studentA.setEnglishGrade(80); studentB.setEnglishGrade(68); studentB.setMathsGrade(50); grade_Total=studentA+studentB; cout<<grade_Total.getMathsGrade()<<endl; cout<<grade_Total.getEnglishGrade()<<endl ; return 0; } 17

Inheritance of overloaded operators Overloaded operators (other than ‘=‘) Are inherited by derived classes, it is Often necessary to redefine their behavior for all classes in hierarchy Maths_grade English_grade Sceince_grade Language grade Technology_grade Student_grade Extended Grade 18

Inheritance Polymorphism (Method Polymorphism) Method Polymorphism refers to the use of same method name in different classes in the class hierarchy for different purposes. Moron orphic function 1-1 relationship Polymorphic methods in 1-m relationship Function ImplementationFunction name 1 Method ImplementationFunction name 1 m 19

example Method ‘getChar’ convert ASCII code for any alphabetical character to its character. Any other ASCII code is convert to a blank character (space) getChar ASCIIChar ShowChar Character 20

#include class ASCIIChar { protected: char character; public: void getChar(int char_number); void showChar(); }; void ASCIIChar::getChar(int char_number) { if((char_number>=65 && char_number =97 && char_number<=122)) { character=char_number; } else{ character=32; } void ASCIIChar::showChar() { cout<<"character is"<<character <<endl;} int main() { ASCIIChar a_char; a_char.getChar(97); a_char.showChar(); return 0; } 21

Inheritance Polymorphism……. Assume that we want to extended this class with a derived class which will store only upper case alphanumerical characters Same name but work different. ‘GetChar’ has different form. getChar ASCIIChar ShowChar Character getChar UpperCase ASCIIChar 22

#include class ASCIIChar { protected: char character; public: void getChar(int char_number); void showChar(); }; class UpperCaseASCIIChar:public ASCIIChar{ public: void getChar(int char_in); }; void UpperCaseASCIIChar::getChar(int char_in){ ASCIIChar::getChar(char_in); if(character !=32){ character -=32;} } void ASCIIChar::getChar(int char_number) { if((char_number>=65 && char_number =97 && char_number<=122)) { character=char_number; } else{ character=32;} } void ASCIIChar::showChar() { cout<<"character is"<<character <<endl;} int main() { ASCIIChar either_case; UpperCaseASCIIChar upper_case; either_case.getChar(97); upper_case.getChar(97); either_case.showChar(); upper_case.showChar(); return 0;} Inherit 23

Namespaces Namespaces allow to group entities like – Classes – Objects – functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The format of namespaces is: namespace identifier { entities } 24

// namespaces #include using namespace std; namespace first { int var = 5; } namespace second { double var = ; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; }