ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
C++ data types. Structs vs. Classes C++ Classes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
CITA 342 Section 1 Object Oriented Programming (OOP)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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:
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Chapter 7: User-Defined Functions II
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?
Abstract Data Types Programmer-created data types that specify
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 9 Classes: A Deeper Look, Part 1
Chapter 9 Objects and Classes
Introduction to Classes and Objects
Classes and Objects.
Defining Classes and Methods
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Presentation transcript:

ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading

Classes and Members In C++ the mechanism to create objects is called the class Member Functions are the way we manipulate objects Classes can support information hiding Programs use classes without knowing how they are implemented

Class Members C++ classes have Member Functions and Member variables Classes can have a public section and a private section Members in the public session can be seen from outside the class Members in the private session can not be seen from outside the class

C++ class syntax (definition) The class head The member list –The public section Member functions (Typically) Member variables (Occasionally) –The private section Member variables (Typically) Member functions (Occasionally)

A common pattern for new classes Public member functions permit programmers to modify and examine objects of the new class Constant public members let a member function examine data (but not modify it) Private member variables store information about the status of an object of the new class (not available outside the class itself)

Using a Class To use a class, we create an instance of the object Each instance has its own private data and its own member functions We activate the member functions to tell the object to perform the behavior [This is sometimes referred to as sending a message to the object]

Implementing a Member Function Member functions are attached to their respective classes with the scope resolution operator (::) The operator is placed between the class name and the function name A function definition looks like this: :: void myclass::myFunction(…) {…};

The Key to Member Variables Each object keeps its own copies of all member variables When a member function’s implementation refers to a member variable, then the actual member variable always comes from the object that activated the member function

Constructors Provides an initialization function that is guaranteed to be called. It is called every time an instance of the class is created. It can have several different number of and type of parameters. Since it operates inside a class, it does not have any return value (no return type allowed)

Constructor (cont) A constructor with no parameters is called the default constructor When a class has no constructor, the compiler automatically creates the automatic default constructor that simply calls the default constructor for member variables that are objects of other classes You should never depend on the automatic default constructor

Parameters Value parameters Reference parameters Constant reference parameters

Inline Member Functions Placing a function definition inside a class definition is a called an inline member function You don’t have to write the implementation later Inline functions can cause some inefficiency, result in messier class definitions and should only be used when the function consists of a single short statement

Header and Implementation Files A header file provides all the information that a programmer needs in order to use the class An implementation file that provides the actual implementations of the class’s member functions

Compiler Directive A compiler directive in the header file, called a macro guard prevents duplicate class definitions #ifndef or (!defined) #define #endif

Value Semantics of a Class The assignment operator copies the values for each member variable. Called the automatic assignment operator. Do not work under certain situations (discussed later on ) A copy constructor is a constructor with exactly one argument that is the same type as the constructors class that creates a new object by coping an object of the same type default is automatic copy constructor

The Constant Declaration A (usually) global definition of a constant that can not change while the program is running A more meaningful way to express a value used throughout a program Localizes the change of the constant to a single point in the program

Classes and Parameters When functions communicate, they do so by using parameters. Understanding the use of parameters is essential for writing correct programs. Default arguments can be listed in the function prototypes of any function. A default constructor may be implemented with default arguments for all of its arguments.

Parameters The function parameter is referred to as the formal parameter. The passed value is the argument, also called the actual parameter or the actual argument.

Value Parameters A value parameter is declared by writing the type name followed by the parameter name. A local copy is made for use by the function. Data passed into a value returning function are always value parameters. Some values passed into a void function may be value parameters

Reference Parameters A reference parameter is declared by writing the type name followed by the character & and the parameter name. Changes made to the formal parameter will change value of the actual parameter in the calling function. Use to pass data out of a function. [data may originate in the function or be changed from data passed into the function]

Constant Reference Parameters A constant reference parameter is a reference parameters declared with the keyword const before the reference parameter. The compiler will not allow code to be written that would attempt to alter the value of the argument.

Operator Overloading C++ lets you define the meaning of many operators for a new class This new definition is called overloading the operator The overloaded function has the name operator e.g. operator == overloads the equivalence operator

Overloading Input and Output Operators The return type is called a reference return type which permits “chaining” of output statements The function returns the same stream it has just written (read)

Friend Functions A friend function is a function that is not a member function, but still has access to the private member variables The functions prototype is proceeded by the keyword friend Friendships should be limited to functions that are written by the programmer who implemented the class

Summary Object-oriented programming supports information hiding by placing data in packages called objects. Objects are manipulated by member functions defined along with their classes.

Summary Abstract Data Types (ADTs) are programmer defined data types. The term abstract emphasizes that the behavior of the ADT is what the user is concerned with. We create objects that exhibit the behavior of the problem we are trying to solve.