Jzyk C++ cz 7. Jarosław Gramacki Instytut Informatyki i Elektroniki



Similar documents
Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

An Incomplete C++ Primer. University of Wyoming MA 5310

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

EP241 Computer Programming

CSE 1020 Introduction to Computer Science I A sample nal exam

Note: Syntactically, a ; is needed at the end of a struct definition.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

C++FA 5.1 PRACTICE MID-TERM EXAM

Visual C Tutorial

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

Answers to Mini-Quizzes and Labs

7.7 Case Study: Calculating Depreciation

Advanced Data Structures

Description of Class Mutation Mutation Operators for Java

Ch 7-1. Object-Oriented Programming and Classes

Appendix M: Introduction to Microsoft Visual C Express Edition

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Coding conventions and C++-style

Binary storage of graphs and related data

The C Programming Language course syllabus associate level

Basics of C++ and object orientation in OpenFOAM

Object Oriented Software Design II

Comp151. Definitions & Declarations

Illustration 1: Diagram of program function and data flow

Object Oriented Software Design II

Object-Oriented Programming Lecture 2: Classes and Objects

Brent A. Perdue. July 15, 2009

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Computer Programming C++ Classes and Objects 15 th Lecture

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Basic Math for the Small Public Water Systems Operator

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

An API for Reading the MySQL Binary Log

Graduate Assessment Test (Sample)

C++ INTERVIEW QUESTIONS

Bevezetés a C++ Standard Template Library-be

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Computer Science K-12

C++ Language Tutorial

C++ Input/Output: Streams

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Surface Area Quick Review: CH 5

1. Polymorphism in C++...2

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Answers to Review Questions Chapter 7

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Fondamenti di C++ - Cay Horstmann 1

El Dorado Union High School District Educational Services

Variable Base Interface

Compile-time type versus run-time type. Consider the parameter to this function:

N3458: Simple Database Integration in C++11

Object Oriented Software Design II

Appendix K Introduction to Microsoft Visual C++ 6.0

Help on the Embedded Software Block

Network Programming. Writing network and internet applications.

Curriculum Map. Discipline: Computer Science Course: C++

Chapter 13 Storage classes

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Answers to Selected Exercises

An Introduction To UML Class Diagrams Classes

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Java Interview Questions and Answers

CISC 181 Project 3 Designing Classes for Bank Accounts

Qt Signals and Slots. Olivier Goffart. October 2013

Basics of I/O Streams and File I/O

C++ Programming Language

Course notes Standard C++ programming

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

C++ Overloading, Constructors, Assignment operator


Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

Graphics Module Reference

CS114: Introduction to Java

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

VB.NET - CLASSES & OBJECTS

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Formatting Numbers with C++ Output Streams

C++ Crash Kurs. C++ Object-Oriented Programming

Data Structures using OOP C++ Lecture 1

The Payroll Program. Payroll

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

CS 111 Classes I 1. Software Organization View to this point:

Transcription:

Jzyk C++ cz 7 Jarosław Gramacki Instytut Informatyki i Elektroniki ) *++, *-./ ) - 0 oblicz_place() 11 virtual 2 3 total += table[ i ]->oblicz_place(); ). Personel::oblicz_place() - 4 5!5% 5 ) / Personel, - -6 - ) 5 5 2 /!- " %!" #$$% 2

) 7 233 3. 2 33. 6 ) 8 5 2 0 9-9 : - -2 -./ - 63-3 / ) ; / 6 3 * 6-6 </ 2 (5 5!" #$$% 3 ) 2 6 3 3 2 3 3 3 2 =>>,! Personel,% ) / < *< ( TablePersonel ) 1=01 6 7 2 3! 6?@ % class Personel { Personel( const char* _nazwisko, short _biuro = 0 ); ~Personel() { delete [] nazwisko; virtual void print() const; // virtual float oblicz_place() const { return 0; // tak było virtual float oblicz_place() const =0; // tak jest teraz: // czysta funkcja wirtualna void set_biuro( short _biuro ) { biuro = _biuro; const char* get_telefon() const; char* nazwisko; short biuro;. 2 A B!" #$$% 4

) 3-6- - 552 ) 5 3 ) 5 9 339-43 - C3/9 5! - % * 2 ". ) " 53 2 =>> ) " )!" #$$% 5 ) -6 ( class Ksztalt // klasa abstrakcyjna { virtual void rysuj() =0; virtual void obracaj (float _kat) =0; virtual void przesun (float _x, float _y) =0;... ; Ksztalt -6 6-2 -- class Okrag : public Ksztalt { void rysuj(); void obracaj (float _kat); void przesun (float _x, float _y); float srodek, promien; class Prostokat : public Ksztalt { void rysuj(); void obracaj (float _kat); void przesun (float _x, float _y); float gorny, lewy, dolny, prawy; : 3 2 5 5 Ksztalt class Kwadrat : public Prostokat {...!" #$$% 6

11 #include <iostream> using namespace std; class CPolygon { protected: int width, height; void set_values (int a, int b) { width = a; height = b; class CRectangle: public CPolygon { int area () { return (width * height); class CTriangle: public CPolygon { int area () { return (width * height / 2); int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << endl; // 20 cout << trgl.area() << endl; // 10 return 0; <!" #$$% 7 11 #include <iostream> using namespace std; class CPolygon { protected: int width, height; void set_values (int a, int b) { width=a; height=b; virtual int area () { return (0); class CRectangle: public CPolygon { int area () { return (width * height); class CTriangle: public CPolygon { int area () { return (width * height / 2); int main () { CRectangle rect; CTriangle trgl; CPolygon poly; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; CPolygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; // 20 cout << ppoly2->area() << endl; // 10 cout << ppoly3->area() << endl; // 0 return 0; 2 virtual CPolygon 3 2!" #$$% 8

) ( // shape.h // Shape abstract-base-class definition. #ifndef SHAPE_H #define SHAPE_H #include <string> using std::string; class Shape { // virtual function that returns shape area virtual double getarea() const; // virtual function that returns shape volume virtual double getvolume() const; // C++ standard string class // pure virtual functions; overridden in derived classes virtual string getname() const =0; // return shape name virtual void print() const =0; // output shape #endif 3 5 5 " "!" #$$% 9 ) ( // shape.cpp // Shape class member-function definitions. #include <iostream> using std::cout; #include "shape.h" // Shape class definition // return area of shape; 0.0 by default double Shape::getArea() const { return 0.0; // return volume of shape; 0.0 by default double Shape::getVolume() const { return 0.0;!" #$$% 10

// point.h // Definition of class Point #ifndef POINT_H #define POINT_H #include "shape.h" // Shape class definition class Point : public Shape { Point( int = 0, int = 0 ); void setx( int ); int getx() const; void sety( int ); int gety() const; // default constructor // set x in coordinate pair // return x from coordinate pair // set y in coordinate pair // return y from coordinate pair // return name of shape (i.e., "Point" ) virtual string getname() const; virtual void print() const; // output Point object int x; int y; #endif!" #$$% 11 ) ( // point.cpp // Member function definitions for class Point #include <iostream> using std::cout; #include "point.h" // Point class definition Point::Point( int xvalue, int yvalue ) : x( xvalue ), y( yvalue ) { void Point::setX( int xvalue ) { x = xvalue; int Point::getX() const { return x; void Point::setY( int yvalue ) { y = yvalue; int Point::getY() const { return y; // override pure virtual function getname: return name of Point string Point::getName() const { return "Point"; // override pure virtual function print: output Point object void Point::print() const { cout << '[' << getx() << ", " << gety() << ']';!" #$$% 12

// circle.h // Definition of class Circle #ifndef CIRCLE_H #define CIRCLE_H #include "point.h" // Point class definition class Circle : public Point { Circle( int = 0, int = 0, double = 0.0 ); void setradius( double ); // set radius double getradius() const; // return radius double getdiameter() const; double getcircumference() const; virtual double getarea() const; // return diameter // return circumference // return area // return name of shape (i.e., "Circle") virtual string getname() const; virtual void print() const; // output Circle object double radius; #endif // Circle's radius!" #$$% 13 // circle.cpp // Member function definitions for class Circle #include <iostream> using std::cout; #include "circle.h" // Circle class definition Circle::Circle( int xvalue, int yvalue, double radiusvalue ) : Point( xvalue, yvalue ) { setradius( radiusvalue ); void Circle::setRadius( double radiusvalue ) { radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); double Circle::getRadius() const { return radius; // calculate and return diameter double Circle::getDiameter() const { return 2 * getradius(); // calculate and return circumference double Circle::getCircumference() const { return 3.14159 * getdiameter(); // override virtual function getarea: return area of Circle double Circle::getArea() const { return 3.14159 * getradius() * getradius(); // override pure virutual function getname: return name of Circle string Circle::getName() const { return "Circle"; // override pure virtual function print: output Circle object void Circle::print() const { cout << "center is "; Point::print(); // invoke Point's print function cout << "; radius is " << getradius();!" #$$% 14

// cylindr.h // Definition of class Cylinder #ifndef CYLINDR_H #define CYLINDR_H #include "circle.h" // Circle class definition class Cylinder : public Circle { Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); void setheight( double ); double getheight() const; virtual double getarea() const; virtual double getvolume() const; // set Cylinder's height // return Cylinder's height // return Cylinder's area // return Cylinder's volume // return name of shape (i.e., "Cylinder" ) virtual string getname() const; virtual void print() const; double height; #endif // output Cylinder // Cylinder's height!" #$$% 15 // cylinder.cpp // Member and friend function definitions for class Cylinder #include <iostream> using std::cout; #include "cylinder.h" Cylinder::Cylinder( int xvalue, int yvalue, double radiusvalue, double heightvalue ) : Circle( xvalue, yvalue, radiusvalue ) { setheight( heightvalue ); // set Cylinder's height void Cylinder::setHeight( double heightvalue ) { height = ( heightvalue < 0.0? 0.0 : heightvalue ); // get Cylinder's height double Cylinder::getHeight() const { return height; // override virtual function getarea: return Cylinder area double Cylinder::getArea() const { return 2 * Circle::getArea() + getcircumference() * getheight(); // override virtual function getvolume: return Cylinder volume double Cylinder::getVolume() const { return Circle::getArea() * getheight(); // override pure virtual function getname: return name of Cylinder string Cylinder::getName() const { return "Cylinder"; // override pure virtual function print: output Cylinder object void Cylinder::print() const { Circle::print(); cout << "; height is " << getheight();!" #$$% 16

// main.cpp (Driver for shape, point, circle, cylinder hierarchy) #include <iostream.h> #include <iomanip.h> #include "shape.h" #include "point1.h" #include "circle1.h" #include "cylindr1.h" void virtualviapointer ( const Shape * ); void virtualviareference ( const Shape & ); int main() { cout << setiosflags( ios::fixed ios::showpoint ) << setprecision( 2 ); Point point( 7, 11 ); // create a Point Circle circle( 3.5, 22, 8 ); // create a Circle Cylinder cylinder( 10, 3.3, 10, 10 ); // create a Cylinder point.printshapename(); // static binding point.print(); // static binding cout << '\n'; circle.printshapename(); // static binding circle.print(); // static binding cout << '\n'; cylinder.printshapename(); // static binding cylinder.print(); // static binding cout << "\n\n";!" #$$% 17 Shape *arrayofshapes[ 3 ]; // array of base-class pointers // aim arrayofshapes[0] at derived-class Point object arrayofshapes[ 0 ] = &point; // aim arrayofshapes[1] at derived-class Circle object arrayofshapes[ 1 ] = &circle; // aim arrayofshapes[2] at derived-class Cylinder object arrayofshapes[ 2 ] = &cylinder; // Loop through arrayofshapes and call virtualviapointer // to print the shape name, attributes, area, and volume // of each object using dynamic binding. cout << "Virtual function calls made off " << "base-class pointers\n"; for ( int i = 0; i < 3; i++ ) virtualviapointer( arrayofshapes[ i ] ); // Loop through arrayofshapes and call virtualviareference // to print the shape name, attributes, area, and volume // of each object using dynamic binding. cout << "Virtual function calls made off " << "base-class references\n"; for ( int j = 0; j < 3; j++ ) virtualviareference( *arrayofshapes[ j ] ); return 0;!" #$$% 18

// Make virtual function calls off a base-class pointer // using dynamic binding. void virtualviapointer( const Shape *baseclassptr ) { baseclassptr -> printshapename(); baseclassptr -> print(); cout << "\narea = " << baseclassptr -> area() << "\nvolume = " << baseclassptr -> volume() << "\n\n"; // Make virtual function calls off a base-class reference // using dynamic binding. void virtualviareference( const Shape &baseclassref ) { baseclassref.printshapename(); baseclassref.print(); cout << "\narea = " << baseclassref.area() << "\nvolume = " << baseclassref.volume() << "\n\n";!" #$$% 19 11 #include <iostream> using namespace std; class CPolygon { protected: int width, height; void set_values (int a, int b) { width=a; height=b; virtual int area (void) =0; void printarea (void) { cout << this->area() << endl; class CRectangle: public CPolygon { int area (void) { return (width * height); class CTriangle: public CPolygon { int area (void) { return (width * height / 2); int main () { CPolygon * ppoly1 = new CRectangle; CPolygon * ppoly2 = new CTriangle; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly1->printarea(); // 20 ppoly2->printarea(); // 10 delete ppoly1; delete ppoly2; return 0; <! % 131 163,!" #$$% 20

- ) - D ) 3-6 5 6 55. <! 2 < / 3 2 53 55% ) 2 /./! 5,% 5! - 5 6 <65% ) 0 3 151 E2 - - 4:F!" #$$% 21