C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

More on Classes Inheritance and Polymorphism
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Overview of C++ Polymorphism
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
OOP Basics Classes & Methods (c) IDMS/SQL News
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Principles of programming languages 10: Object oriented languages
Chapter - 4 OOP with C# S. Nandagopalan.
Chapter 16: Classes and Data Abstraction
Inheritance and Polymorphism
Object-Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Object-Oriented Programming
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming
Overview of C++ Polymorphism
CIS 199 Final Review.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University

Outline User defined types Public, Private, and Protected Constructors and Destructors Encapsulation Inheritance Polymorphism Operator Overloading

User defined types When solving problems often times it becomes easier for programmers to express algorithms in terms of real world objects or complex types Ex. vectors, matrices, polygons, etc Ex. Cars, planes, and trains Types define both the internal data and operations which can be done on the data Ex. A vector might hold an x and a y data and define operations such vector addition and subtraction C++ allows for complex types in the form of classes and structs

Abstract types and Hierarchies Often many types have things in common, e.g., all shapes can be drawn or all shapes have color However, you cannot concretely define a shape. In this case a shape is abstract We derive more concrete classes off of abstract ones, e.g., a circle is a concrete shape Shape CircleSquareTriangle

Class Example Can use struct or class to define a new type struct Point { float m_x, m_y; void Set(float x, float y){ m_x = x; m_y = y; } }; class Point { float m_x, m_y; public: Point(float x, float y) : m_x(x), m_y(y) {} void Set(float x, float y){ m_x = x; m_y = y; } };

Public, Protected, and Private scopes Public: functions and data which any part of a program may access Ex. Data Accessors – GetData(), modifiers – SetData(newData), operations – ActionX() Protected: functions and data which only the class and any derived subclasses may access Ex. Helper functions for operations. Most data in class hierarchies Private: functions and data which only the class itself may access Ex. Helper functions and some data By default: classes have private scope while structs have public scope

Constructors and Destructors Constructors – “functions” which define how data is initialized Same name as the class itself Default constructor – takes no parameters, e.g., Point() Copy constructor – takes an object of the class itself to initialize the data, e.g., Point(oldPoint) Destructors – define how data is deleted when an object goes out of scope Same name as the class itself except with a ~, e.g., ~MyClass() Usually not called in code you write. The compiler automatically calls it when an object goes out of scope.

Encapsulation A mechanism for restricting access to some of the object's components A construct that facilitates the bundling of data with the methods (or other functions) operating on that data Advise: Helper functions and data should be “Encapsulated”, i.e., should be private or protected Use Accessors and Modifiers to “Get” and “Set” the data Bundle operations on an object inside the class, not as utility functions

Inheritance Inheritance is a construct allowing reuse of code in existing objects and definition of subtypes Gives rise to class hierarchies In the shape example, Circle, Square, and Triangle all “inherited” color from Shape Base class: class which is inherited from, e.g., Shape Derived class: class which does the inheriting, e.g., Circle By inheriting, derived classes have access to public and protected data and functions of the base class

Inheritance Example class Shape{ protected: Color m_c; public: Shape(Color c) : m_c(c) {} void Draw(){ //do nothing} }; class Circle : public Shape { float m_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} void Draw() { //draw circle of radius r} }; class Square : public Shape { float m_side; public: Square(Color c, float s) : Shape(c), m_side(s) {} void Draw() { //draw circle of radius r} }; Notice: How to inherit a class How to call the base class constructor Data is inherited

Polymorphism Defining and constructing data and algorithms in terms of abstract classes At runtime the specific type of the class is decided and its behavior is executed Abstract classes provide an interface to express algorithmic use, whereas derived classes provide the concrete implementation Abstract classes use “virtual” functions to ensure subtypes implement the correct interface

Abstract Classes Abstract classes describe an interface “virtual” is a keyword used to allow subtypes to overload the function in the manner they see fit “pure virtual” functions MUST be overloaded in the base class class Shape { virtual void DrawNotPureVirtual(){//do nothing} virtual void DrawPureVirtual() = 0; }; class Circle{ //virtual void DrawNotPureVirtual() does not need to be defined. By inheritance it will do nothing virtual void DrawPureVirtual() {//draw my circle} };

Polymorphism in Algorithms As stated before polymorphism allows algorithms to be expressed in terms of abstract classes Utilizes pointers (discussed in future review in more depth) and virtual lookup at runtimevirtual lookup at runtime At runtime the concrete type’s implementation is used void DrawAllShapes(Shape** shapes, int numShapes){ for(int i = 0; i<numShapes; i++){ //virtual function call. Lookup of concrete implementation occurs at runtime shapes[i]->Draw(); }

Operator Overloading Operator overloading provides a way for commonly used operators to be applied to user defined types E.g., assignment (=), addition (+), input (>>) output (<<) For more information class MyClass{ string m_name; public: MyClass(string name) : m_name(name) {} MyClass& operator=(const MyClass& m){ m_name = m.m_name; }

Exercises 1.Create a struct called Point. This will be a 2D point. Provide a constructor, overloaded assignment operator, and public data. Remember: Test your program incrementally after implementing each item to check correctness. 2.Create an abstract class called Shape. Shape will have protected data of a Point describing where the shape is and a pure virtual function Print(). 3.Create subclasses of Shape, Circle and Square with private data and overloading the virtual Print functions. 4.Create a main function instantiating a Square and a Circle as an abstract Shape. Call the virtual function of Shape.