CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Road Map Introduction to object oriented programming. Classes
CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Lecture 9 Concepts of Programming Languages
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
 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.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Classes, Interfaces and Packages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
A First C++ Class – a Circle
Review: Two Programming Paradigms
About the Presentations
More about OOP and ADTs Classes
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Introduction to Classes
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Recitation Course 0603 Speaker: Liu Yu-Jiun.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Creating and Using Classes
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CS-212 Intro to C++

Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis on “what” not “how” Information Hiding to lower level of complexity Preconditions and Postconditions

Pre and Postconditions Preconditions: the current state of the object – ex. The object exists and has a nonzero value Postconditions: the new state of the object after the function has been performed – ex. The object still exists and has increased in value by a factor of ten are described for every operation belonging to the object

C++ Class An implementation of an ADT A user defined “type” Encapsulation of Data and Function – Data and its manipulators must always be kept together

Class Consists of : – an interface definition (fn.h header file) Data definition Definition of user interface – an implementation (fn.cpp code file) code for user interface code for helper functions – Could both be in same file but that would defeat the purpose of information hiding

Class Interface Definition class statement class name private: section – data content of the class – statements ahead of “public” are private – private functions are for use of the class not the class user protected: section (for base classes only) – data content of the base class (more later...) public: section – prototype statements for member functions – publishes the user interface to the class

Using a class Must first instantiate (make an instance of) an object of type class (ex. throttle fuel_control) Tell the object what you want it to do by using the “.” operator (dot operator). – fuel_control.shut_off() (note: specify the object and the action you want performed) – if the object returns a value assign it current = fuel_control.flow();

Class Constructors Assign initial values to an object Never returns a value Same name as the class Guaranteed to be called (automatic) upon instantiation of an object of type class

Default Constructor If no constructor is found – a default constructor, that does no initialization is used when the object is defined (only reserves the storage defined in the private section)

Object Initialization a single constructor may be used that assigns values to all private variables The constructor can be overloaded to provide flexibility to the user – each overload must have unique signature Good idea to always provide a default constructor that assigns some value to all private variables

Destructors Automatically run when an object of type class goes out of scope use to clean up after the object (return dynamically allocated storage to the storage management system Same name as the class, but preceded by a ~ no overloading allowed never returns a value not needed for statically allocated storage

Member Functions Also known as “setters” and getters” provide the class’ behavior you should be a “setter” function and a getter” function for each piece of private data (also known as “state data”)

Time class definition class Time { public: Time ( ) ; /* default constructor */ Time ( unsigned H, unsigned M, char AorP ) ; Time ( unsigned mil) ; Time (const Time & oTime ); /* copy constructor */ void SetHour(unsigned H ) ; void SetMinute ( unsigned M) ; void Setam_pm) (char C); void SetMiltime(); unsigned GetHour(); unsigned GetMinute(); unsigned Getam_pm (); unsigned GetMiltime() void Display ( ostream & out) ; private: unsigned hours, minutes ; char am_pm ; unsigned miltime ; }

Time class implementation Time::Time() // Constructor for the Time class { // initialize time to midnight hours = 0 ; minutes = 0; am_pm = “A”; miltime = 0; } Time::Time ( unsigned H, unsigned M, char AorP ) ; { hours = H ; minutes = M am_pm = AorP; if (am_pm ==“P”) miltime = ((hours+12)*100)+minutes; else miltime = hours*100 + minutes; }

Time class implementation Time:: Time ( unsigned mil) { hours = mil / 100 if (hours > 12) { hours = hours - 12; am_pm = “P”; } else am_pm = “A”; minutes = mil % 100; miltime = mil; }

Time Class Copy Constructor Time::Time(const Time & oTime) { hours = oTime.hours; min = oTime.min; am_pm = oTime.am_pm; miltime = oHours.miltime; }

Inline Member Functions If a member function is simple; define it inline in the class definition (header file) (provide the function body {.....} inline) Compiler recompiles the inline code each time it is called and places the code inline (like a C macro) – faster code – uses more storage Functions may also be “inlined” by using the Inline keyword

namespaces using namespace std; The concept of namespaces was added to C++ in the last release of the specification namespaces allow you to assign portions of your program to different namspaces, variable names are local so they may be reused with in a program as long as the uses are in different namespaces the namespace “std” is a little special – you don’t have to specify the “.h” for all standard #includes – #include