Constructors Member Functions Inheritance

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.
More on Objects CS 102 More, more, more on Objects.
C++ data types. Structs vs. Classes C++ Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE122 Feb. 22, Any question on Vehicle sample code?
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 Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
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.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Classes C++ representation of an object
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.
Chapter 9 More on Objects and Classes
A First C++ Class – a Circle
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
More about OOP and ADTs Classes
This technique is Called “Divide and Conquer”.
Introduction to Classes
Lecture 22 Inheritance Richard Gesick.
Simple Classes in C# CSCI 293 September 12, 2005.
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
CS148 Introduction to Programming II
Introduction to Classes and Objects
Constructors Member Functions Inheritance
CIS 199 Final Review.
Class.
Classes C++ representation of an object
C++ Programming CLASS This pointer Static Class Friend Class
CPS120: Introduction to Computer Science
Separating Interface from Implementation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Constructors Member Functions Inheritance Classes - Part II Constructors Member Functions Inheritance

Classes Structured Programming 256 Chapter 9

Class Implementation A constructor is a special member function provided to allow initialization of variables.

Constructors constructor initializes private data when a class object is created Example TimeType(int, int, int); Student(double, char, char, char, char);

Default Constructors default constructor parameterless constructor, it initializes the variables to some default value, such as zero Example TimeType(); Student();

Default Constructor Function same Syntax Classname::Classname( ) { function body } *

Constructor Functions do NOT have a return type (not even void) must have the same name as the class itself invoked when you declare an instance of a class with a list of initial values there should be a default constructor * * * *

Default Constructor Function Example 1 TimeType::TimeType( ) { hrs = 0; mins = 0; secs = 0; } Example 2 Student::Student( ) { GPA = 0; num_of_grades = 130; } * *

Default Constructor Function Example 3 Circle::Circle( radius) { radius_of circle = radius; x_center = 5; y_center = -3; } Example 4 Student::Student( ) { } * *

Destructors ~Classname( ) A default do-nothing destructor is provided by the compiler. Only one destructor per function class No arguments - no return values

Some Terminology OOP C++ object class object or class instance instance variable private data member method public member function message passing function call (to a public member function)

Review - In Specification File default constructor Classname(); Track(); constructor Classname(type para, type para , ...); Track(int disc, double hurdles); member function type Function(type para , type para , ...); void Event(double hurdles); h file * * *

Review - In Implementation File constructor Classname::Classname(type para, type para , ...) Track::Track(int disc, double hurdles) member function type Function(type para , type para , ...) void Event(double hurdles) default constructor Classname::Classname() { } Track::Track() { } all have a body * *

Review - In Client File default constructor If NOT passed parameters - gets called when declaring an object of type Class. constructor If passed parameters - gets called when declaring an object of type Class. member function object.Function(para , para , ...); b_jenner.Event(double hurdles); * * *

Inheritance creating new classes out of existing ones reuse code without need for retesting and validation speeds up programming process simple vs. multiple

Inheritance - simple Circle base class: derived class: Sphere Cylinder *

Inheritance - multiple base classes: derived class: Car Truck Minivan *

Constructor Function Call Syntax Classname object(arguments); or Classname object = Classname(arguments); * *

Constructor Function Call Example Circle big_circ(0, 2. 3.6); Circle big_circ = Circle(2, 2, 3.6); Circle big_circ( ); } Circle big_circ; * * * *

Overload Constructors Date me; Date you(7,25,49); Date moon(690628); You must provide constructors for each. Prototypes Headers Date( ); Date::Date() Date(int, int, int); Date::Date(int, int, int) Date(long) ; Date::Date(long)

Class Implementation: Function Syntax Example double Circle::setVal(int xx, int yy, double rr) { xcenter = xx; ycenter = yy; radius = rr; } * *

Function Call Syntax object.function(arguments); Example a.setVal( ); b.setVal(3, 2, 6.5); big_circ.setVal(1, 4, 8.3) sm_circ.setVal( ); k.setVal(1, 0, 5.0); * *

Initialize Data Members of a Class Student iago; iago.ID = 12345; double checkAmt; checkAmt = 54.78; * *

Arrays of Objects Date theDate[4]; creates the objects: theDate[0]

Arrays of Objects Function calls: thedate[0].showdata(); or for(ndx=0; ndx<4; ndx++) theDate[ndx].showdata();

Common Errors missing ; at end of class declaration including a return type with constructor’s prototype missing a return type with function prototypes defining more than one default constructor per class

Common Errors missing :: and class name in header forgetting to include the appropriate header files