group work #hifiTeam

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
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.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
 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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
C++ Review (3) Structs, Classes, Data Abstraction.
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+
 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.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
CONSTRUCTOR AND DESTRUCTORS
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Introduction to Object Oriented Programming Chapter 10.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
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.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Static data members Constructors and Destructors
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.
Programming with ANSI C ++
CISC181 Introduction to Computer Science Dr
Constructors & Destructors.
Chapter 5 Classes.
Constructor & Destructor
Constructors & Destructors
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
This pointer, Dynamic memory allocation, Constructors and Destructor
Lecture Constructors and Destructors
Dynamic Memory Allocation Reference Variables
Constructor & Destructor
Introduction to Classes
Contents Introduction to Constructor Characteristics of Constructor
Constant pointers and pointers to constants
Constructors and destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Constructors & Destructors
CS 144 Advanced C++ Programming April 30 Class Meeting
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Constructor 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.

General Syntax of Constructor Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below: { arguments}; The default constructor for a class X has the form X::X()

Cont…… The constructor is automatically called when an object is created. There are several forms in which a constructor can take its shape namely: Default Constructor Parameterized Constructors Copy constructor

Some important points about constructor: Automatically called when an object is created. We can define our own constructors A constructor takes the same name as the class name. We can’t define a constructor in the private section.

Destructor Destructors are special member functions. The destructor is a method called when the object is destroyed. Release dynamic allocated memory. Destructors are automatically named. Takes the same name of class name.

General Syntax of Destructor ~ class name();

Some important points about destructor: Take the same name as class name. Defined in the public. Destructors cannot be overloaded. No return type is specified.

COMMENT TO EACH LINE OF CODE Example of destructor using namespace std; class City { //within a class public: string cityName; //code to run on object deletion int population; City(String cityName, int population) { this->cityName = cityName; this->population = population; } ~City() { cout << "This city is being destroyed" << endl; }

Example of constructor using namespace std; class City { public: string cityName; //fields int population; City() { //constructor if no input parameters are provided this->cityName = "No city name provided"; this->population = 0; }

Cont……… City(String cityName) { //constructor if only a city name is provided this->cityName = cityName; //this keyword distinguishes the city Name's this->population = 0; } City(String cityName, int population) { //constructor if both parameters provided this->cityName = cityName; this->population = population; } }

Parameterized Constructor: A parameterized constructor is just one that has parameters specified in it. We can pass the arguments to constructor function when object are created. A constructor that can take arguments are called parameterized constructors.

Example class Creature { private: int year Of Birth; public: // … Creature(int year) { //Parameterized Constructor year Of Birth = year; } };

Copy Constructor: Copy Constructor is used to declare and initialize an object from another object. The process of initializing through a copy constructor is known as copy initialization.