0% found this document useful (0 votes)
41 views

C Lecture-1-13

1. The document defines a hierarchy of shape classes including Square, Rect, and Circle. 2. Square, Rect, and Circle inherit from the abstract base class Shape. 3. Methods like square(), perim(), printOn(), and multBy() are declared virtual in Shape and defined in the derived classes.

Uploaded by

Olexii Samoyil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

C Lecture-1-13

1. The document defines a hierarchy of shape classes including Square, Rect, and Circle. 2. Square, Rect, and Circle inherit from the abstract base class Shape. 3. Methods like square(), perim(), printOn(), and multBy() are declared virtual in Shape and defined in the derived classes.

Uploaded by

Olexii Samoyil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

13.

1. 2. 3. 4. 5. 6. . . . . . , . : , , , . . ,
y y y

. . .

. . , , . , .

: , ,

, , . : ( ( ( , , , , . . ++ , ).

); ); , . . : , , . , -

. , 1000). , , .

. , . . , , get- set, . "shape.h"

. ( ,

, .

, . .

//
#include <iostream> #include <iomanip> usingstd::ostream; classSquare {private: intx,y; // // //

doublea; // protected : // doublegetA() const { return a; } public: Square(double side=1., intCx=100, int Cy=100) : x(Cx),y(Cy),a(side) {}; virtual ~Square() {} // intgetX() const { returnx; } // intgetY() const { return y; } voidmoveTo(intCx, int Cy); virtual double square() const; // virtual double perim() const; // virtual void printOn(ostream&) const; Square&multBy(double k); // friend ostream& operator<<(ostream&os, const Square& s); // }; ostream& operator<<(ostream&os, const Square& s); // ":"

classRect : publicSquare // , { // public private: doubleb; // public: Rect(double sideA=1., double sideB=1., intCx=100, int Cy=50) : Square(sideA,Cx,Cy), b(sideB) {} // virtual double square() const; // virtual double perim() const; virtual void printOn(ostream&) const; // friend ostream& operator<<(ostream&os, constRect& s); // };

Square. (private) , . , Square , ( . getX, getY, moveTo , . virtual ( : ). multBy , . . , . , , Rect : Rect. Rect . mySquare.multBy(2.).printOn(cout). , Square, k . , ). : . , ( , . , , , , . ), . . getA. (protected). . Square , . , , ( ++ ), ,

.
// "shape.cpp"

. . ( ).

#include "shape.h" // void Square::moveTo(intCx, int Cy) { if (Cx<0) x = 0; else if (Cx>1000) x = 1000; else x = Cx; if (Cy<0) y = 0; else if (Cy>1000) y = 1000; else y = Cy; } double Square::square() const { return a*a; } double Square::perim() const { return a*4.; } Square& Square::multBy(double k) { if (k>=0) a *= k;else a *= -k; return *this; } void Square:: printOn(ostream&os) const { os<< "Square of side " <<std::fixed <<std::setprecision(1) <<a<<';'; } // ostream& operator<<(ostream&os, const Square& s) { s.printOn(os); returnos; } // doubleRect::square() const { returngetA ()*b; } // doubleRect::perim() const { return (getA()+b)*2.; } voidRect::printOn(ostream&os) const { os<< "Rectangle of size " <<std::fixed <<std::setprecision(1) <<getA() <<" X "<<b<<';'; } /* ostream& operator<<(ostream&os, const Square& r) { os<< "Squar e of side " <<std::fixed <<std::setprecision(1) <<a<<';'; returnos; } ostream& operator<<(ostream&os, constRect& r) { os<< "Rectangle of size " <<std::fixed <<std::setprecision(1) <<r.getA() <<" X "<<r.b<<';'; returnos; }*/

. , . , . , ,
Square A; Re ctB(5.5);

, , ,

, ++ ( ObjectPascal) . , . , .

Square* S[5];

cout<<A<<' \n'<<B<<'\n';

. . : , . , ++ , .

, , , operator<<. . ,

S .

: : RectB; Square&refS=B;. . ,

#include "shape.h" usingstd::cout; double ratio(const Square&); constint n = 5;

//

int main() { Square A; RectB(5.5); Square C(10.7,50,50); // cout<<A<<' \n'<<B<<'\n'<<C<<"\n\n"; Square* S[n]; // S[0] = &A; S[1] = &B; S[2] = &C; // S[3] = new Square(7.5,200,10); // S[4] = new Rect(1.,45); double r[n]; for (int i=0; i<n; i++) // { r[i] = ratio(*S[i]); cout<<'#'<<i<<" : " <<*S[i]<< " ratio = "<<std::setprecision(4)<<r[i]<<' \n'; } int k = 0; for (int i=1; i<n; i++) if (r[k]<r[i]) k=i; cout<<"\nExtrashape : "<<*S[k]<< " with ratio = "<<std::setprecision(4)<<r[k]<<" \n\n"; double p = 1./r[k]; for (int i=0; i<n; i += 2) // { S[i]->multBy(p).printOn(cout); cout<< '\n'; } return 0; } double ratio(const Square& S) { returnS.square()/S.perim(); }

Square of side 1.0; Rectangle of size 5.5 X 1.0; Square of side 10.7; #0 #1 #2 #3 #4 : : : : : Square of Rectangle Square of Square of Rectangle side 1.0; ratio = 0.2500 of size 5.5 X 1.0; ratio = 0.4231 side 10.7; ratio = 2.6750 side 7.5; ratio = 1.8750 of size 1.0 X 45.0; ratio = 0.4891

Extrashape : Square of side 10.7; wi th ratio = 2.6750 Square of side 0.4; Square of side 4.0; Rectangle of size 0.4 X 45.0;

, ? , ? , . , , .
#include<iostream> #include<iomanip> usingstd::ostream;

, , , : Rect . , , . , Shape, . , Square, Rect Circle Shape , , , : . , Square

isa ( . ? . , . .

). , !

, . . .

//

"nshape.h"

classShape {private: intx,y; public: Shape(intCx, int Cy) : x(Cx),y(Cy) {}; virtual ~Shape() {} // intgetX() const { return x; } intgetY() const { return y; } voidmoveTo(intCx, int Cy); virtual double square() const = 0; // virtual double perim() const = 0; // virtual Shape&multBy(double k) = 0; // virtual void printOn(ostream&) const; }; ostream& operator<<(ostream&os, const Shape& s);

class Square : public Shape {private: double a; public: Square(double side=1., intCx=100, int Cy=100) : Shape(Cx,Cy),a(side) {}; virtual double square() const; virtual double perim() const; virtual void printOn(ostream&) const; virtual Square&multBy(double k); }; classRect : public Shape {private: double a, b; public: Rect(double sideA=1., double sideB=1., int Cx=100, int Cy=50) : Shape(Cx,Cy), a(sideA), b(sideB) {} virtual double square() const; virtual double perim() const; virtual void printOn(ostream&) const; virtualRect&multBy(double k); }; class Circle : public Shape {private: double r; public: Circle(double radius=1., int Cx=50, int Cy=100) : Shape(Cx,Cy), r(radius) {} virtual double square() const;

virtual double perim() const; virtual void printOn(ostream&) const; virtual Circle&multBy(double k); };

, .

Shape , square, perim =0. , ). ( ,

multBy ,

: ( ). multBy. ( .) . , , -

#include "nshape.h" #define _USE_MATH_DEFINES // , #include <cmath> ostream& operator<<(ostream&os, const Shape& s) { s.printOn(os);return os; } // ----Shape ----void Shape::moveTo(intCx, int Cy) { if (Cx<0) x = 0;else if (Cx>1000) x = 1000;else x = Cx; if (Cy<0) y = 0;else if (Cy>1000) y = 1000;else y = Cy; } void Shape::printOn(ostream&os) const // { os<< " shape situated at ("<<x<<','<<y<<')'; } // ----Square ----double Square::square() const { return a*a; } double Square::perim() const { return a*4.; } Square& Square::multBy(double k) { std::cout<<" - - - Square::multBy()"; if (k>0) a *= k;else if (k<0) a *= -k; return *this; } void Square::printOn(ostream&os) const { os<<"Square of side "<<std::fixed<<std::setprecision(1)<<a<<','; Shape::printOn(os); // os<<';'; } // ----Rect ----doubleRect::square() const { return a * b; } doubleRect::perim() const { return (a + b) * 2.; } Rect&Rect::multBy(double k) { std::cout<<" - - - Rect::multBy( )"; if (k>0) { a *= k; b *= k; }else if (k<0) { a *= -k; b *= -k; } return *this; } voidRect::printOn(ostream&os) const { os<<"Rectangle of size "<<std::fixed<<std::setprecision(1)<<a<<" X "<<b<<','; Shape::printOn(os); os<<';'; // } // ----Circle ----double Circle::square() const { return M_PI*r*r; } double Circle::perim() const { return 2.*M_PI*r; } Circle& Circle::multBy(double k) { std::cout<<" - - - Circle::multBy()"; if (k>0) r *= k;else if (k<0) r *= -k; return *this; }

void Circle::printOn(ostream&os) const { os<<"Circle of radius "<<std::fixed<<std::setprecision(1) <<r<<','; Shape::printOn(os); os<<';'; // }

printOn .
#include "nshape.h" #include <fstream> usingstd::cout; double ratio(const Shape&); constint n = 5;

:: ( (). ):

int main() { Square A; RectB(5.5); Circle C(10.7,50,50); cout<<A<<' \n'<<B<<'\n'<<C<<"\n\n"; Shape* S[n]; S[0] = &A; S[1] = &B; S[2] = &C; S[3] = new Square(7.5,200,10); S[4] = new Circle(31.5,45); double r[n]; for (int i=0; i<n; i++) { r[i] = ratio(*S[i]); cout<<'#'<<i<<" : "<<*S[i]<< " ratio = "<<std::setprecision(4)<<r[i]<<' \n'; } int k = 0; for (int i=1; i<n; i++) if (r[k]<r[i]) k=i; cout<<"\nExtrashape : "<<*S[k]<< "\n with ratio = "<<std::setprecision(4)<<r[k]<<" \n\n"; S[k]->printOn(cout); cout<< '\n' << S[k] ->multBy(0.5) << ' \n'; std::ofstream f("result.txt"); for (int i=0; i<n; i++) f<<'#'<<i<<" : "<<*S[i]<<" ratio = "<<std::setprecision(4)<<r[i]<<' \n'; f.close(); return 0; } double ratio(const Shape& S) { returnS.square()/S.perim(); }

Square of side 1.0, shape situated at (100,100); Rectangle of size 5.5 X 1.0, shape situated at (100,50); Circle of radius 10.7, shape situated at (50,50); #0 #1 #2 #3 #4 : : : : : Square of Rectangle Circle of Square of Circle of side 1.0, shape situated at (100,100); ratio = 0.2500 of size 5.5 X 1.0, shape situated at (100,50); ratio = 0.4231 radius 10.7, shape situated at (50,50); ratio = 5.3500 side 7.5, shape situated at (200,10); ratio = 1.8750 radius 31.5, shape situated at (45,100); ratio = 15.7500

Extrashape : Circle of radius 31.5, shape situated at (45,100); with ratio = 15.7500 Circle of radius 31.5, shape situated at (45,100); Circle of radius 15.8, shape situated at (45,100); - - - Circle::multBy()

, , .

, , ( . . , ,
y y y y

, . ) . , . ( ) ; ; ; . ) . . : : , ), . , , , . , , , ). ( . ( , , . ,

, ,

. .

( const , 1. . ( 2. ). . , 3. ( ObjectPascal) ). . Deriv , Base, . . . , , .

++

Deriv&Deriv::operator=(constDeriv&inst) { if (this == &inst) return *this; Base::operator=(inst) // field = ... // return *this; }

. , . ,

You might also like