C++ (classes) Hwansoo Han

Similar documents
Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Introduction to Programming Using Java (98-388)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

And Even More and More C++ Fundamentals of Computer Science

G Programming Languages - Fall 2012

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Data Abstraction. Hwansoo Han

More C++ : Vectors, Classes, Inheritance, Templates

Object Oriented Software Design II

Object Oriented Software Design II

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Java Object Oriented Design. CSC207 Fall 2014

Short Notes of CS201

CMSC 132: Object-Oriented Programming II

CS201 - Introduction to Programming Glossary By

Inheritance, and Polymorphism.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Ch02. True/False Indicate whether the statement is true or false.

Lecture 5: Inheritance

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object-oriented Programming. Object-oriented Programming

Advanced Systems Programming

Object Oriented Programming

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 14 Abstract Classes and Interfaces

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Chapter 6 Introduction to Defining Classes

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS

CLASSES AND OBJECTS IN JAVA

CS 162, Lecture 25: Exam II Review. 30 May 2018

COEN244: Class & function templates

Data Structures and Other Objects Using C++

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout

Chapter 5 Names, Bindings, Type Checking, and Scopes

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

VIRTUAL FUNCTIONS Chapter 10

CS 237 Meeting 18 10/22/12

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Instance Members and Static Members

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

OBJECT ORİENTATİON ENCAPSULATİON

Memory management COSC346

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming. Solved MCQs - Part 2

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CS250 Final Review Questions

C++ Inheritance and Encapsulation

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

Polymorphism Part 1 1

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

CS105 C++ Lecture 7. More on Classes, Inheritance

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

G52CPP C++ Programming Lecture 13

Mobile Application Programming. Swift Classes

ECE 3574: Dynamic Polymorphism using Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CS-202 Introduction to Object Oriented Programming

Inheritance, Polymorphism, and Interfaces

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Polymorphism

Class, Variable, Constructor, Object, Method Questions

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Programming, numerics and optimization

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

index.pdf January 21,

CMSC 132: Object-Oriented Programming II

Inheritance, Polymorphism and the Object Memory Model

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

Implementing Abstract Data Types (ADT) using Classes

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

An Introduction to C++

10. Abstract Data Types

C++ Important Questions with Answers

ITI Introduction to Computing II

C++ Inheritance and Encapsulation

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

ITI Introduction to Computing II

CS250 Final Review Questions

Chapter 4 Java Language Fundamentals

Chapter 3 Objects and Classes

Instantiation of Template class

CSE 307: Principles of Programming Languages

7. C++ Class and Object

Chapter 11. Abstract Data Types and Encapsulation Concepts

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Lecture 3. Variables. Variables

Assumptions. History

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Miri Ben-Nissan (Kopel) (2017)

Object-Oriented Programming Concepts

Transcription:

C++ (classes) Hwansoo Han

Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2

Base Class: shape Members of a class Methods : rotate(), move(), Shape(), ~Shape() Attributes: _center, _color class Shape { Point _center; Color _color; Shape (Point& pos = Point(), Color& c = Black) { _center = pos; _color = c ; virtual ~Shape(void) { ; virtual void rotate(double angle) = 0; virtual void move(point& p) { _center = p; ; 3

Subclass: triangle A new class is derived from an existing class New class existing class subclass base class Inherits all attributes and methods from the base class Virtual functions can be defined differently in subclasses 4 class Triangle : public Shape { private: Point vertices[3]; Triangle (Point p[3]); virtual ~Triangle(void) { ; virtual void rotate(double angle) { ; virtual move(point& p) { vertices[0] = p; ; _center, _color, vertices[3] Method list - Triangle(); - ~Triangle(); - rotate(); - move();

Virtual Functions Methods in C++ are non-virtual by default Derived subclass has the same functions as the base class Cannot be changed in subclass [NOTE] methods in Java are all virtual by default. Virtual methods are specified with a virtual in C++ virtual func_name (arg_list) { ; Virtual methods can be changed in subclass Virtual means overridable E.g. move(), rotate() in Shape and Triangle 5

Abstract Class Base class shape is an abstract class At least one pure virtual method (abstract method) exists virtual func_name (arg_list) = 0; Abstract method MUST be virtual Abstract method MUST be defined in a subclass Cannot instantiate an object of an abstract type Shape a_shape; // ERROR Triangle a_triangle; // OK 6

Constructor A special method Constructor is called to initialize when an object is created classname(arg_list) { ; Cannot be virtual, Cannot return any value, Automatically called Can have several constructors with different argument types class Triangle: class Shape { private: Point vertices[3]; Triangle (Point& p[3]) { ; Triangle (Point& c) vertices({c,c,c) { ; 7

Constructor If you omit constructor, compiler provides a default one Triangle::Triangle( ) { Once you specify any constructor, default constructor should be provided by programmers class Triangle: { // no constructor ; int main() { Triangle t1; 8 // OK class Triangle: { Triangle (Point& p[3]) { ; ; int main() { Triangle t1; Triangle t2(p); // ERROR // OK

Destructor Another special method Destructor is called to clean up when its lifetime ends [virtual] ~classname() { ; Can be virtual, if one of other methods is virtual Can have only one destructor, cannot return any value Useful to use if dynamic memory is assigned as a part of the object Destructor is called by delete For a local variable, destructor is called when the function returns Point ps[3] = {Point(0,0), Point(1,1), Point(1,0); 9 Func() { Triangle t(ps); Triangle *pt = new Triangle(ps); Triangle &rt = new Trianlge(ps); delete pt;

Scope Default scope of fields and methods are private in C++ [NOTE] In Java, default is package (accessible within a file) Three scopes in C++ Private : accessible only within the same class Protected : accessible from the same class and subclass Public : accessible from anywhere 10

Location of Objects Object is allocated in stack or heap Defined as a local variable e.g. Triangle t; Created with new e.g. Triangle *pt = new Triangle; Global object is placed in data section and initialized (with constructor) when program starts Defined as an external variable e.g. Triangle gt; // global var [NOTE] In Java, all objects of non-primitive types are allocated in heap! Primitive types in Java: char, short, int, long, float, double, No delete all garbage collected 11

Keyword this Keyword this refers to the object itself Instance variables can be accessed only with their names But they can be hidden by argument names Keyword this is useful in such case class Point { // declaration of class Point private: int x, y; Point(int, int); ; 12 Point::Point(int x, int y) // Point Constructor { this->x = x; this->y = y;

Operator Overloading Redefine the meaning of operator for class type objects class Point { // declaration of class Point int x, y; double operator-(const Point & p) const; ; double Point::operator-(const Point & p) const { double dist; int xdiff = x p.x, ydiff = y p.y; return dist = sqrt( xdiff*xdiff + ydiff*ydiff ); does not change invoking object 13 int main() { Point p1 (0,0), p2 (1,1); // tow Point objects double dist = p1 p2; // p1.operator-(p2);