Constructors for classes

Similar documents
3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object Oriented Design

Recharge (int, int, int); //constructor declared void disply();

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ 8. Constructors and Destructors

CS313D: ADVANCED PROGRAMMING LANGUAGE

And Even More and More C++ Fundamentals of Computer Science

6.096 Introduction to C++ January (IAP) 2009

Constructors.

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Protection Levels and Constructors The 'const' Keyword


THE BIG FOUR FRIEND FUNCTIONS

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Intermediate Programming, Spring 2017*

Lecture 18 Tao Wang 1

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Computer Programming with C++ (21)

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Abstract data types &

Abstract Data Types & Object-Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

CS304 Object Oriented Programming Final Term

Operator overloading. Conversions. friend. inline

CS201 Some Important Definitions

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

Introduction to Classes

Introduction Of Classes ( OOPS )

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Ch. 12: Operator Overloading

G52CPP C++ Programming Lecture 13

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 10 Introduction to Classes

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Class, Variable, Constructor, Object, Method Questions

Special Member Functions

Data Structures using OOP C++ Lecture 3

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Object-Oriented Programming

Object-Oriented Principles and Practice / C++

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Constructor - example

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Chapter 11. Abstract Data Types and Encapsulation Concepts

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

An Introduction to C++

Cpt S 122 Data Structures. Introduction to C++ Part II

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Software Design and Analysis for Engineers

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Lecture 5. Function Pointers

Review: C++ Basic Concepts. Dr. Yingwu Zhu

cout<< \n Enter values for a and b... ; cin>>a>>b;

Friend Functions and Friend Classes

Object Oriented Design

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Constructors & Destructors

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

CONSTRUCTOR AND DESTRUCTOR

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Derived Classes in C++

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Overview of C Feabhas Ltd 2012

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Advanced Systems Programming

Object Oriented Programming(OOP).

COMP 110/L Lecture 19. Kyle Dewey

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Values and Variables 1 / 30

CIS 190: C/C++ Programming. Classes in C++

CMSC 132: Object-Oriented Programming II

Functions and Recursion

Scope. Scope is such an important thing that we ll review what we know about scope now:

More C++ : Vectors, Classes, Inheritance, Templates

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Introductory ios Development

OBJECT ORIENTED PROGRAMMING USING C++

Transcription:

Constructors for Comp Sci 1570 Introduction to C++

Outline 1 2 3 4 5 6 7

C++ supports several basic ways to initialize i n t nvalue ; // d e c l a r e but not d e f i n e nvalue = 5 ; // a s s i g n i n t nvalue = 5 ; // copy i n i t i a l i z a t i o n i n t nvalue ( 5 ) ; // d i r e c t i n i t i a l i z a t i o n i n t v a l u e {5}; // uniform i n i t. (C++ 11 o n l y ) MyClass my o b j e c t ; // d e c l a r e but not d e f i n e // i n i t i a l i z a t i o n? Even though direct form looks a lot like a function call, the compiler keeps track of which names are variables and which are functions. Direct can perform better than copy for some data types. It also helps differentiate from assignment.

Outline 1 2 3 4 5 6 7

Constructors To be able to initialize your user-defined types, you must write a special kind of member function for your class. It is called a. Constructors are really rather special in that they have some key features: Constructors are always named the name of the class (with the same capitalization) Constructors have no return type (not even void) and hence has no return statement. Constructors can be overloaded like any other function, and you will most likely do so. Constructors are called by the compiler automatically; they are rarely called explicitly by the programmer. If you write no, the compiler will provide your class with a default. The mechanism is suppressed once you write any.

Outline 1 2 3 4 5 6 7

Outline 1 2 3 4 5 6 7

A that accepts no parameters is known as default. If no is defined then the compiler supplies a default.... C i r c l e : : C i r c l e ( ) { r a d i u s = 0 ; } i n t main ( ) { C i r c l e m y c i r c l e ; m y c i r c l e. r a d i u s =4;...

Outline 1 2 3 4 5 6 7

A that receives arguments/parameters, is called parameterized.... C i r c l e : : C i r c l e ( double r ) { r a d i u s = r ; } i n t main ( ) { C i r c l e m y c i r c l e ( 4 ) ;...

Outline 1 2 3 4 5 6 7

A that initializes an object using values of another object passed to it as parameter, is called copy. It creates the copy of the passed object.... C i r c l e : : C i r c l e ( C i r c l e &o t h e r C i r c l e ) { r a d i u s = o t h e r C i r c l e. r a d i u s ; } i n t main ( ) { C i r c l e m y c i r c l e ( 4 ) ; C i r c l e m y c i r c l e 2 ( m y c i r c l e ) ;...

Outline 1 2 3 4 5 6 7

A destructor is a member function having sane name as that of its class preceded by (tilde) sign and which is used to destroy the objects that have been created by a. It gets invoked when an object s scope is over. C i r c l e ( ) {} We don t cover these this semester

Outline 1 2 3 4 5 6 7

Like any other function, a can also be overloaded with different versions taking different parameters: with a different number of parameters and/or parameters of different types. The compiler will automatically call the one whose parameters match the arguments

Outline 1 2 3 4 5 6 7

When a is used to initialize other members, these other members can be initialized directly, without resorting to statements in its body, by inserting, before the s body, a colon (:) and a list of s for class members. For members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified. If non-static const data members in your class have default and you don t use a initializer list, you won t be able to initialize them to intended state as they will be initialized to their default state. Reference data members must be intialized when compiler enters as references can t be just declared & initialized later. This is possible only with initializer list. Efficiency: rather than calling default init just to overwrite later, initialize directly

c l a s s R e c t a n g l e { i n t width, h e i g h t ; p u b l i c : R e c t a n g l e ( int, i n t ) ; i n t a r e a ( ) { return width h e i g h t ; } } ; R e c t a n g l e : : R e c t a n g l e ( i n t x, i n t y ) { width=x ; h e i g h t=y ; } // or R e c t a n g l e : : R e c t a n g l e ( i n t x, i n t y ) : width ( x ) { h e i g h t=y ; } // or R e c t a n g l e : : R e c t a n g l e ( i n t x, i n t y ) : width ( x ), h e i g h t ( y ) { }

Outline 1 2 3 4 5 6 7

Outline 1 2 3 4 5 6 7

c l a s s MyClass { p u b l i c : i n t a, b ; s t r i n g c ; } ; MyClass : : MyClass ( const MyClass& e x i s t i n g O b j ) : a ( x. a ), b ( x. b ), c ( x. c ) {} i n t main ( ) { MyClass...

Outline 1 2 3 4 5 6 7

provided by default (copy assignment) MyClass foo ; // o b j e c t i n i t i a l i z a t i o n : copy c o n s t r u c t o r MyClass bar ( foo ) ; // o b j e c t i n i t i a l i z a t i o n : copy c o n s t r u c t o r MyClass baz = foo ; // o b j e c t a l r e a d y i n i t i a l i z e d : copy a ssignment foo = bar ; Body of overloading function is similar to copy

Outline 1 2 3 4 5 6 7

A class may contain other as member variables. By default, when the outer class is constructed, the member variables will have their default called. This happens before the body of the executes.