Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
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.
C++ data types. Structs vs. Classes C++ Classes.
Chapter 15: Operator Overloading
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CS212 Programming-II for Engineers Spring
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
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.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Introduction to Object Oriented Programming Chapter 10.
Learners Support Publications Constructors and Destructors.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 13: Overloading and Templates
Overloading Operator MySting Example
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
CS212 Programming-II for Engineers
Introduction to Classes
Classes and Data Abstraction
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Chapter 11: Inheritance and Composition
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Classes C++ representation of an object
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
More C++ Classes Systems Programming.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Object Oriented Programming C++

ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing how the functions and data are related An ADT implementation has several parts: – interface function-names (methods) – operators (possibly re-defined symbols) – data Any/all of these may be public or private 2

Classes Collection of data elements and functions Functions are called "methods" or members of the class – Tied to the data – May be hidden from other functions Member access – exposed by "public" names – Hidden by "private" names 3

Classes - example class Complx {public: float my_real_part; float my_imag_part; };//  note the semicolon!!!!! Using "public" makes all elements "visible" Using "private", would make them visible ONLY to methods (functions) inside the class (not shown). NOTE: looks a lot like a struct 4

Program structure – pt 2 - C++ Usually, put the class definition in a header file #include it later in BOTH : – the implementation file where the methods are defined – the user-program file where methods get used 5

Objects An object is just a struct with members Default operations (members) – Constructor – Destructor – Assignment – Copy YOU must provide any initialization code – in a constructor YOU must provide return values (if any) YOU must free any dynamic storage 6

Constructors C++ provides a "default" constructor Only works if no parameters: Complx X; Fails for uses like: Complx X(3,4); – variables get initialized – other setup must be done IF you create a constructor, you MUST specify the Default Constructor too or it won't exist – Complx( ) { }; – no actual code needed inside the { } 7

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

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

C++ console I/O basic operators – << the "insertion" operator – >> the "extraction" operator stream names for standard I/O – cin is the input "file" – cout is the output "file" usage – cin>>x;// get a value – cout<<x;// output a value 10

Operators Ordinary variables have basic operators built-in + - * / % | & || && New objects (structs, classes) may need more How do you "add" 2 complex numbers? Given: complex#'s a and b; – what does a+b mean??? – compiler knows nothing about complex #'s Define a NEW action to perform "+" – but continue to use the "+" operator!!!! – add the real parts, add the imaginary parts – store each result in the proper answer-part. 11

Operators – 2 Often, we "want" to re-use an operator – i.e.; give a new meaning to "+" – this is called "overloading" let "+" mean "add complex numbers" as well as add integers, floats, etc. c=a+b; // a, b & c are Complex May need a "friend" function, due to flaws in the C++ object model "friend" functions do not get the leading "classname::" syntax 12

More on Overloading = overload this as a member function == != = <> overload as a non-member (friend function) returning a boolean >> << (insertion and extraction) overload as non-members (friends) returning type iostream + - * / % (arithmetic) as overload non- members += and -=... overload same as + and - cannot overload ::. sizeof ?:

Polymorphism (multiple forms) x=sqrt(y); // what data type is "y"? – could be: float, int, double, real How does compiler know how to handle the incoming data? An ADT allows us to create MULTIPLE methods – all have the same name – all have different parameter types – compiler links caller's code & the "right one" based on parameter types. – Object writer creates these multiple methods 14

Operators Ordinary variables have basic operators built-in + - * / % | & || && New objects (structs, classes) may need more How do you "add" 2 complex numbers? Given: Complex#'s a and b; – what does a+b mean??? – compiler knows nothing about Complex #'s Define a NEW action to perform "+" – add the real parts, add the imaginary parts – store each result in the proper answer-part. 15

Example – Declare the methods class Complx { private:// hidden member data float my_real_part, my_imag_part; // below are member functions (i.e.; "methods") public: Complx (float a, float b); // initializing constructor Complx( ) { }; // default constructor Complx get_real(return my_real_part); // not shown Complx operator + (Complx); // defines "+" as a member name // define what output means friend ostream &operator<<(ostream &xout, const Complx &C ) { xout << C.my_real_part <<"+"<< C.my_imag_part<<"j"; return output; } }; // orange: declare parts of the class, green: use it, red: return-value 16

Example- implementation // :: means the name after :: is a member of the class "Complx" Complx :: Complx (float a, float b) // implement constructor {my_real_part=a; my_imag_part=b;} Complx Complx ::operator+ (Complx C) {Complx tmp; // need a place for output tmp.my_real_part = my_real_part + C.my_real_part; tmp.my_imag_part = my_imag_part + C.my_imag_part; return tmp; } // orange: declare parts of the class, green: use it, red: return-value 17

"friend"s friend ostream &operator<<(ostream &xout, const Complx &C ) { xout << C.my_real_part <<"+"<< C.my_imag_part<<"j"; return output; } What does it do? Remember that we needed to define + for complex numbers? Now need to define how to output them Output functions don't understand class data So we have now overloaded: – the + operator – the << operator 18

"friend"s - pt-2 Access private and protected members NOT a member of the class Allows operations across multiple classes << is a member of the iostream class Want << able to output private data in Complx 19

Example-pt 2 void main() { Complx A(2,2); Complx B(1,1),C; C=A+B; } 20