1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
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,
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ data types. Structs vs. Classes C++ Classes.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
ECE122 Feb. 22, Any question on Vehicle sample code?
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 10.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Classes (Chapter ) Classes (Chapter ) Data with natural operations Classes Using Classes.
Learning Objectives Pointers as dada members
Submission Example May 14, 2018
Chapter 7: User-Defined Functions II
C++ Programming Functions
Programming with ANSI C ++
A First C++ Class – a Circle
CISC181 Introduction to Computer Science Dr
C++ Programming Functions
Constructor & Destructor
group work #hifiTeam
Name: Rubaisha Rajpoot
Constructors and Other Tools
COMS 261 Computer Science I
C++ Constructor Insanity CSE 333 Summer 2018
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
Object oriented programming (OOP) Lecture No. 6
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
C++ data types.
Object Oriented Programming (OOP) Lecture No. 10
Presentation transcript:

1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27

2 Announcements

3 Review Classes –User defined data types

4 Outline Classes

vec.h: definition file vec.cpp: implementation file

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; // default constructor (no arguments) VEC();

Default constructor Implementation: vec.cpp #include VEC::VEC() { x = 0.0f; y = 0.0f; }

Application #include using namespace std; #include int main ( ) { VEC v1; return 0; }

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; // default constructor (no arguments) VEC(); // output values in object void print(); float getX(); void setX(float val);

Accessor Implementation float VEC::getX (){ return x; } void VEC::setX (float val) { x = val; }

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval);

Implementation Definition: goes in vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; }

VEC Class Definition: goes in vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval); VEC(float xval);

Implementation Definition: vec.cpp VEC::VEC(float xval) { x = xval; y = 0.0; }

VEC Class It may be more useful to add a constructor that takes one parameter and optionally takes another parameter –Default parameter –An initial value for x –Optional parameter for y –Allows us to construct VEC objects as: VEC v1(1.2); –Also called and initial value constructor

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval); VEC(float xval, float yval = 0.0f); Default value for yval if not specified

Implementation Run CodeWarrior vec05 Definition: vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; } Default values are not specified in the implementation file

Implementation Syntax error –Improper member function overloading –Compiler cannot distinguish between the two parameter and the two parameter with an initial value constructor –Keep the constructor that provides flexibility

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval = 0.0f); Default value for yval if not specified Run CodeWarrior vec06

VEC Class If one default parameter is a good idea, how about a constructor with two default parameters –Optional parameter for x and y Run CodeWarrior vec07

VEC Class The copy constructor –We would like to provide the flexibility of creating a copy of a VEC object VEC v1(1.2f, 3.4f); VEC v2(v1); –Include a constructor that makes a copy of an existing VEC object

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval = 0.0f, float yval = 0.0f); VEC(VEC v); Run CodeWarrior vec08

VEC Class The copy constructor –If you do not implement a copy constructor, one will be implemented for you –By the compiler –It may not do what you want It will for this course, but not for COMS 262!!

VEC Class The assignment operator –We would like to provide the flexibility of assigning one VEC object to another VEC v1(1.2f, 3.4f); VEC v2; V2 = v1; –Overload the assignment operator so a VEC can appear on the left and right side

VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval = 0.0f, float yval = 0.0f); VEC(const VEC& v); Run CodeWarrior vec09 VEC& operator=(const VEC& v);