Namespaces and Class Hierarchies

Similar documents
Inheritance and Polymorphism

Inheritance, and Polymorphism.

Chapter 20 - C++ Virtual Functions and Polymorphism

Chapter 19 C++ Inheritance

Chapter 19 - C++ Inheritance

Programming II Lecture 2 Structures and Classes

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Data Structures (INE2011)

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

pointers & references

Inheritance Examples

Abstract Data Types (ADT) and C++ Classes

Object oriented programming

Midterm Exam 5 April 20, 2015

Object-Oriented Programming

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

What is an algorithm?

C++ Inheritance. Dr. Md. Humayun Kabir CSE Department, BUET

CS 11 C++ track: lecture 1

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

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

Chapter 1: Object-Oriented Programming Using C++

Fig Fig Fig. 10.3

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

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

Sample Code: OUTPUT Daily Highs & Lows

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

Assumptions. History

Object Oriented Programming in C++

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Templates and Vectors

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

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

CS11 Intro C++ Spring 2018 Lecture 1

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

C++ (classes) Hwansoo Han

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

Topic 7: Algebraic Data Types

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Inheritance (Deitel chapter 9)

Programming in C++: Assignment Week 5

Appendix M: Introduction to Microsoft Visual C Express Edition

IS 0020 Program Design and Software Tools

Chapter 9 - Object-Oriented Programming: Polymorphism

Use the template below and fill in the areas in Red to complete it.

Unified Modeling Language a case study

IS 0020 Program Design and Software Tools

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CSCI 102 Fall 2010 Exam #1

Intermediate Programming, Spring 2017*

Module 7 b. -Namespaces -Exceptions handling

Programming Language. Functions. Eng. Anis Nazer First Semester

ITI Introduction to Computing II

CS 162 Intro to CS II. Structs vs. Classes

ITI Introduction to Computing II

Class Sale. Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill

Fig. 9.1 Fig. 9.2 Fig. 9.3 Fig. 9.4 Fig. 9.5 Fig. 9.6 Fig. 9.7 Fig. 9.8 Fig. 9.9 Fig Fig. 9.11

ECE 3574: Dynamic Polymorphism using Inheritance

ITI Introduction to Computing II

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

Come and join us at WebLyceum

CSc 328, Spring 2004 Final Examination May 12, 2004

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

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Lecture #1. Introduction to Classes and Objects

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

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

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Chapter 11. The first objective here is to make use of the list class definition from a previous lab. Recall the class definition for ShapeList.

Polymorphism Part 1 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS24 Week 3 Lecture 1

Object oriented programming

Introduction to C++: Part 4

VALLIAMMAI ENGINEERING COLLEGE

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

CS11 Intro C++ Spring 2018 Lecture 3

C++ Constructor Insanity

CS250 Final Review Questions

Programming, numerics and optimization

Intermediate Programming, Spring 2017*

CS

Assignment 3: Inheritance

Introduction to C++ (Extensions to C)

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Partha Sarathi Mandal

Casting and polymorphism Enumeration

System Programming. Practical Session 9. C++ classes

CSCI 104 Polymorphism. Mark Redekopp David Kempe

การทดลองท 8_2 Editor Buffer Array Implementation

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS 106B Lecture 27: Inheritance and Polymorphism in C++

Chapter 15 - C++ As A "Better C"

Transcription:

and 1 2 3 MCS 360 Lecture 9 Introduction to Data Structures Jan Verschelde, 13 September 2010

and 1 2 3

Suppose we need to store a point: 1 data: integer coordinates; 2 functions: get values for the coordinates and a to_string() method. We start with a point in the plane and define a class Point.

the file point1.h #ifndef POINT1_H #define POINT1_H #include <string> class Point public: Point(int a, int b); int get_x() const; int get_y() const; std::string to_string() const; private: }; int x; /* x-coordinate of the point */ int y; /* y-coordinate of the point */

#include "point1.h" #include <sstream> the file point1.cpp Point::Point(int a, int b) x = a; y = b; } int Point::get_x() const // Point::get_y() // is similar return x; } std::string Point::to_string() const std::ostringstream s; s << ( << x <<, << y << ) ; return s.str(); }

testing the class #include "point1.h" #include <iostream> using std; int main() Point p(2,3); cout << "A point with coordinates " << p.to_string() << "." << endl; } return 0;

and 1 2 3

using a Suppose we want also points in 3-space, okaytohavepoint2.h and point3.h, but like to call a type Point. We use s D2 and D3. #include "point2.h" #include "point3.h" int main() D2::Point p(2,3); D3::Point q(2,3,1); If only D3 is used, do using D3.

D2 D2 in the file point2.h class Point public: Point(int a, int b); int get_x() const; int get_y() const; std::string to_string() const; private: int x; /* x-coordinate of the point */ int y; /* y-coordinate of the point */ }; // end of class definition }; // end of

D3 D3 in the file point3.h class Point public: Point(int a, int b, int c); int get_x() const; int get_y() const; int get_z() const; std::string to_string() const; }; }; private: int x; /* x-coordinate of the point */ int y; /* y-coordinate of the point */ int z; /* z-coordinate of the point */

using declaration and directive In point2.cpp we put the using declaration: using D2; and point3.cpp starts with using D3. To use console output cout, without using std, then we must include the using directive using std::cout With using directives we can selectively include names from a.

and 1 2 3

Shape Rectangle Circle Triangle VariableCircle A shape represents a planar geometrical figure. The abstract class Shape stores the name. An object of the class VariableCircle has a center and a radius that can change.

#include <string> class Shape private: std::string label; }; the abstract class Shape public: Shape(std::string name) label = name; } std::string get_name() const return label; } void relabel(std::string name) label = name; } virtual double area() const = 0; virtual void read() = 0; virtual std::string to_string() const = 0;

inheriting from Shape #include "shape.h" #include <string> class Rectangle : public Shape protected: // visible to derived double width, height; public: Rectangle() : Shape("") width = 0.0; height = 0.0; } Rectangle(std::string name, double w, double h) : Shape(name), width(w), height(h) } }; double area() const; void read(); std::string to_string() const;

the file rectangle.cpp #include "rectangle.h" #include <sstream> #include <iostream> double Rectangle::area() const return width*height; } With this definition we override the pure virtual area function of the class Shape.

prompting for a rectangle Observe the difference between a using declaration and a using directive. void Rectangle::read() using std; // using declaration using std::string; // using directive string name; // if no directive, std::string } cout << " give name : "; cin >> name; this->relabel(name); cout << " give width : "; cin >> width; cout << "give height : "; cin >> height; Note: the pointer *this refer to the object.

string representation Useful to define the operator<<: std::string Rectangle::to_string() const std::ostringstream s; s << "rectangle " << this->get_name() << " has width " << width << " and height " << height; } return s.str();

#include "rectangle.h" using std; int main() Rectangle r("a",2,1.23); working with rectangles cout << r.to_string() << endl; cout << "area of " << r.get_name() << " is " << r.area() << endl; shows $ /tmp/test_shape rectangle A has width 2 and height 1.23 area of A is 2.46 $

pointing to a shape Dynamic creation of a rectangle requires a pointer and calling new: Rectangle *t; t = new Rectangle(); cout << "reading second rectangle..." << endl; t->read(); cout << "area of " << t->get_name() << " is " << t->area() << endl; Note: t->read() equals (*t).read().

and 1 2 3

polymorphism We cannot allocate an object of class Shape because Shape is an abstract class. Shape *s; // cannot allocate // s = new Shape("some shape); s = t; cout << "second rectangle again..." << endl; cout << s->to_string() << endl; but we can access the rectangle t via a pointer to an object of the class Shape.

the Circle class Area πr 2 of a circle with radius r requires GNU C mathematical library: #include <cmath> double Circle::area() const return M_PI*pow(radius,2.0); } Where M_PI approximates π and pow computes r 2. The rest of the Circle class is very similar to Rectangle.

make and makefile For larger programs, the make facility is handy: $ make test_shape executes g++ -c rectangle.cpp circle.cpp g++ -o /tmp/test_shape rectangle.o \ circle.o test_shape.cpp as defined in the makefile test_shape: g++ -c rectangle.cpp circle.cpp g++ -o /tmp/test_shape rectangle.o \ circle.o test_shape.cpp Notice the TABs before the g++.

test_shape continued... Circle *c; c = new Circle("C",1.0); cout << c->to_string() << endl; cout << "area of " << c->get_name() << setprecision(17) << " is " << c->area() << endl; s = c; cout << "getting circle again..." << endl; cout << s->to_string() << endl; With the pointer s we can access once a rectangle and once a circle.

and 1 2 3

A computer consists of processor, clock speed, #cores; memory, capacity, and speed. multiple inheritance Processor + speed + cores Memory + capacity + speed Computer

the class Processor class Processor public: Processor(double s, int c); double get_speed() const; int get_cores() const; std::string to_string() const; private: }; double speed; /* clock frequency */ int cores; /* number of cores */

the class Memory class Memory public: Memory(double s, int c); int get_speed() const; int get_capacity() const; std::string to_string() const; private: }; int speed; /* clock frequency */ int capacity; /* capacity in Gb */

the class Computer #include "processor.h" #include "memory.h" #include <string> class Computer : public Processor, public Memory public: Computer(double ps, int pc, int ms, int mc) : Processor(ps,pc), Memory(ms,mc) } std::string to_string() const; };

the main program #include "computer.h" #include <iostream> using std; int main() Computer c(2.26,2,1067,2); cout << "your computer is " << endl; cout << c.to_string() << endl; shows your computer is Processor : 2.26 Ghz, 2 cores Memory : 2 GB, 1067 MHz

and 1 2 3

Ambiguities and Refactoring inheritance may lead to inefficiencies and confusion. For example: an employee has name, address, salary, hours; a student has name, address, major, gpa. A student worker is an employee and a student. Given the Employee and Student, the class StudentWorker inherits from Employee and Student. Problem: name and address stored twice. Refactoring: store common data of Employee and Student in one class Person.

Summary + Assignments Ended Chapter 3: and. are important because we may want to compare our own implementations of a vector, stack, list, stack, queue, tree, map, with what is in the standard template library of C++. Assignments: 1 Make two Vector, one for the plane and another for 3-space, with respective s PlanarVectors and SpatialVectors. In addition to the constructor, provide a method to compute the length of a vector and a to_string() method. Write code to test the two. 2 Add a class Triangle as a third class inheriting from Shape. Give code to test the area function.

more assignments 3 Define the insertion operator<< for objects in the shape hierarchy. Which class will you change? Give code for a test program to show your << works on rectangles and circles. 4 Make a class VariableCircle, inheriting from Circle. Objects of the VariableCircle class have an origin (by default at (0.0,0.0)) and their radius can change. Write code to test the operations of this class. 5 Extend our computer model via a class Disk with similar members as Processor and Memory. Modify the class Computer so it inherits from Disk.