Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Classes. COMP104 Lecture 25 / Slide 2 Motivation  Types such as int, double, and char are simple objects. * They can only answer one question: “What.
1 CS 201 Introduction to c++ (2) Debzani Deb. 2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file.
Classes. COMP104 Class / Slide 2 Motivation  Types such as int, double, and char are “stupid” objects. * They can only answer one question: “What value.
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Accessor Functions 04/13/11. Encapsulation  Like case on my watch Protect from outside Protect from outside Have member functions control data Have member.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading Customised behaviour of operators Unit - 06.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Objects and Classes
Pointer to an Object Can define a pointer to an object:
Chapter 5 Classes.
14.4 Copy Constructors.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
null, true, and false are also reserved.
Default Arguments.
NAME 436.
C++ Programming CLASS This pointer Static Class Friend Class
Chapter 11 Classes.
Presentation transcript:

Constructors 11/10/10

Today  Use constructors to initialize objects.  Use const to protect data members.

Constructors

Constructor  Public member functions that initialize their objects  Automatically called when object declared

Constructor Syntax  Always has the same name as the class  Definition doesn't have a void or return type

class Thermometer { public: Thermometer (double d, char s); //Constructor void input_T(); void rise(double incr); private: double degrees; char scale; //F or C };. Thermometer::Thermometer(double d, char s)‏ { degrees = d; scale = s; }

//Default Constructor -- Always Provide Thermometer::Thermometer()‏ { degrees = 0.0; scale = 'C'; } sec6.2/tconstr.cpp

Another Example  rectangle3.cpp  const protects the data members from change. Use it when a function doesn't change data members. Use it when a function doesn't change data members.

Constructors  What would constructors for gastank.cpp be?  What function could be constant functions?  What about dog.cpp? Add constructors Add constructors What functions should be constant. What functions should be constant.  Do exercises, p , #1-3