Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.

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.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
. Copying, casting, and more. Example: MyString u Lets put our knowledge of C++ classes to use u Define a class to represent a string u Replace all the.
Constructors & Destructors Review CS 308 – Data Structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
OOP Languages: Java vs C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 9 Classes: A Deeper Look, Part I Part II.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
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.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Learning Objectives Pointers as dada members
Review What is an object? What is a class?
Programming with ANSI C ++
Concepts of Constructors and Its Types
Constructors & Destructors.
Constructor & Destructor
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Constructor & Destructor
Inheritance Using work from:
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Destruction and Copying
9-10 Classes: A Deeper Look.
Destruction and Copying
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Rule of Three Part 1 & 2.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

csi2172 class 5 Midterm: June 12

constructor Special method used to create objects of the class Never has a return type. Is called automatically upon the creation of the object. Main purpose is to pre-define the object's data members A properly written constructor will leave the object in a 'valid' state.

copy constructor Its purpose is to: Create the copy on the stack of the object passed Create the copy on the stack of the object returned Create a copy of the object

Accepts an object of the class itself as its argument Typically Z::Z(const Z&). A copy constructor is used for initialization of objects of type T with objects of type T. If a copy constructor is not declared for a class, memberwise initialization is used.

Assignment operator For a class, = is by default defined member-wise assignment; if necessary, the writer of a class can define it differently.class member

Assignment operator class student { private: char * name; int stno;... public: student& operator=(const student&);... };

student& student::operator=(const student& s) { if (&s == this) return *this; delete [] name; name = new char[strlen(s.name) + 1]; strcpy(name,s.name); return *this; }

destructor member of a class used to clean up before deleting an object.memberclassobject It's name is its class' name prefixed by '~'.name For example, Foo's destructor is ~Foo(). Often used to release resources. A destructor is implicitly called whenever an object goes out of scope or is deleted.resourcescope

Operator overload operator overloading means having more than one operator with the same name in the same scope. Built-in operators, such as + and *, are overloaded for types such as int and float.operatorname scopetypeintfloat Users can define their own additional meanings for user-defined types.user-defined type It is not possible to define new operators or to give new meanings to operators for built-in types.built-in type The compiler picks the operator to be used based on argument types based overload resolution rules.compilerargumentoverload resolution

Class review from class vector

The assignment

Inheritance

/* abstract class shape */ class shape { protected: bool filled; public: /* abstract draw method */ virtual void draw(canvas&) const = 0; virtual ~shape() { } void set_filled(bool); };

/* a concrete shape */ class polygon : public shape { protected: point * vertices; int n; public: polygon(int); /* copy constructor */ polygon(const polygon&); virtual ~polygon(); /* destructor */ /*assignment operator*/ polygon& operator=(const polygon&); void add_point(const point&); /* an actual implementation */ virtual void draw(canvas&) const; };

void polygon::draw(canvas& C) const { for(int i=1; i< n; ++i) C.draw_line(vertices[i-1],vertices[i]); C.draw_line(vertices[i-1]); /* slow */ if (filled) C.flood_fill(inside_point(vertices,n)); }

/* a refinement */ class rectangle : public polygon { public: rectangle(const point&, const point&); void draw(canvas&) const; };

void rectangle::draw(canvas& C) const { C.draw_line(vertices[0],vertices[1]); C.draw_line(vertices[1],vertices[2]); C.draw_line(vertices[2],vertices[3]); C.draw_line(vertices[3],vertices[0]); /* fast */ if (filled) C.rect_fill(vertices[0],vertices[3]); }

The midterm