Classes.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
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.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
1 Object-Oriented Programming Using C++ CLASS 27.
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.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own 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?
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
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.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 C++ Classes and Data Structures Course link…..
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Programming Abstractions
C++ Memory Management – Homework Exercises
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Andrew(amwallis) Classes!
Procedural and Object-Oriented Programming
A CLASS CONSISTS OF VARIABLES,
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Chapter 1.2 Introduction to C++ Programming
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?
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
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?
Chapter Structured Types, Data Abstraction and Classes
More about OOP and ADTs Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Separate Compilation and Namespaces
Introduction to Classes
Initialization List.
More about OOP and ADTs Classes
Separating Definition & Implementation
C++ Interlude 1 C++ Classes
Comments, Prototypes, Headers & Multiple Source Files
Code Organization CSCE 121 J. Michael Moore.
Friends, Overloaded Operators, and Arrays in Classes
CS150 Introduction to Computer Science 1
Classes C++ representation of an object
Review: C++ class represents an ADT
What Is? function predefined, programmer-defined
CS 144 Advanced C++ Programming February 21 Class Meeting
CMSC 202 Lesson 8 Classes II.
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Classes

Warmup class Thing { public: void foo(int a) {cout << “A”;} void foo(double a) {cout << “B”;} }; int main() Thing a(); a.foo(1); } What will this script output?

Warmup 2 void foo() { cout << “B”; } class Thing { public: void foo() {cout << “A”;} void bar() {foo();} }; int main() Thing a(); a.bar(); What will this script output?

Header Files In order to promote reusability of code, it is common to put user-defined classes and functions into header files. This way if another person wants to reuse classes or functions, they can simply include the header file. This is done using #include “filename.h” which pastes the contents of the file filename.h into a program

Separating Interface and Implementation It is also common to put declarations into a .h file and put the associated definitions into .cpp file of the same name. If we define a Person class, we would be the declarations into a person.h file and the definitions into a person.cpp file. We would include person.h in our person.cpp file

Header Files Always put #pragma once at the beginning of a header file. Otherwise, if multiple files include the header file in a project, there are redefinition problems. Alternatively can put #ifndef FILENAME_H #define FILENAME_H at the start and #endif at the end

Problem: Implementing a Date class Suppose we wanted to implement a class which represents dates, e.g., 10/23/1997. Things to consider when designing this class: What data variables would we want an object of this class to have? What methods should an object of this class have?

Problem: Date Class Implement a Date class which has 3 data variables (day, month, year) and the following methods A constructor that accepts three input variables (corresponding to the month, day, and year) A default constructor that accepts no input variables and sets the date to 1/1/1970 One accessor function for each of our private data variables. A mutator function that inputs three variables (corresponding to the day, month, and year) and sets the stored date equal to that input. A procedure that prints the stored date to the console in the format MM/DD/YYYY.

Problem: Equality Testing Write a method for Date equals which accepts a Date object and returns true if they are equal. Date date1(1,6,1999), date2(1,7,1999); date1.equals(date2) // should be false What should the header of the method look like? What should be in the body? Why can’t we just do date1 == date2?

Operator Overloading: == We can define == for our Date class. bool Date::operator==(const Date &date) const { return equals(date); }