Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Operator Overloading Fundamentals
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
C++ Review (3) Structs, Classes, Data Abstraction.
1 CSC241: Object Oriented Programming Lecture No 06.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
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 COP3330 / CGS5409.  Aggregation / Composition  Dynamic Memory Allocation.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
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.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
CISC181 Introduction to Computer Science Dr
CS410 – Software Engineering Lecture #11: C++ Basics IV
Constructor & Destructor
Lesson 14 Copy and Assignment
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Allocation Reference Variables
Object Oriented Programming (OOP) Lecture No. 9
Constructors and Destructors
Indirection.
Constructors and Destructors
Object-Oriented Programming (OOP) Lecture No. 22
CS410 – Software Engineering Lecture #5: C++ Basics III
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
SPL – PS3 C++ Classes.
Presentation transcript:

Review of Last Lecture

What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under a "private" section? How many different constructors one class can have? How to initialize member variables in a constructor? How to manage dynamic memory allocation? Why do we need destructor and how to define it? Using "const" in class and define member functions with "const" type. Why do we need that? Using "static" members in class. Why do we need that? Overloaded operator functions. Why do we need that? What is the difference when overloading operator functions as the member functions vs. non-member functions? Friend functions (especially for overloaded operators). How do we define them? Why do we need that?

class Student { private: int peopleSoft; float score; public: void input(int number1,float score1){ peopleSoft =number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following definition and initialization of the objects legal or not based on the previous definition?

class Student { private: int peopleSoft; float score; public: void input(int number1,float score1){ peopleSoft =number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following definition and initialization of the objects legal or not based on the previous definition? Yes.

class Student { private: int peopleSoft; float score; public: Student(int number1, float score1): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following still legal?

class Student { private: int peopleSoft; float score; public: Student(int number1, float score1): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following still legal? No, there is no a default constructor defined!

class Student { private: int peopleSoft; float score; public: Student(int number1=0, float score1=0): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2;

#include class clock { private: int hour, minute, second; public: clock(int h, int m=0, int s=0); void set(int h, int m, int s); void display( ); void ~clock(); }; clock(int h, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } void clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } What is the output?

#include class clock { private: int hour, minute, second; public: clock(int h, int m=0, int s=0); void set(int h, int m, int s); void display( ); void ~clock(); }; clock(int h, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } void clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } It cannot be compiled!

#include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } What is the output?

#include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } Call constructor 6:30:0 12:0:0 Call destructor

#include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } Is the above calculation valid or not?

#include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } //overload + operator! const clock operator +(const &clock1, const &clock2) { return clock(clock1.hour+clock2.hour, clock1.minute+clock2.minute clock1.second+clock2.second); } Will the above work?

#include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); friend const clock operator +(const &clock1, const &clock2); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } //overload + operator! const clock operator +(const &clock1, const &clock2) { return clock(clock1.hour+clock2.hour, clock1.minute+clock2.minute clock1.second+clock2.second); } set it as a friend function!

class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; }

class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; } The above assignment will fail. To address that, we need to implement the overloaded function for the operator ‘=‘.

class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; } The above assignment will fail. Because the default assignment operator will not automatically take care of the memory allocation for pointer type of member variable. So what could happen here?