DATA ABSTRACTION IN C PLUS PLUS
3.3 Pure Virtual Function and Abstract Superclass
An unadulterated virtual capacity is determined by putting "= 0" (called unadulterated specifier) in its announcement. For instance, virtual twofold getArea() = 0;/Pure virtual capacity, to be executed by subclass An unadulterated virtual capacity ordinarily has no usage body, in light of the fact that the class doesn't have the foggiest idea how to actualize the body. A class containing at least one unadulterated virtual capacity is called a theoretical class. You can't make cases from a theoretical class, since its definition might be deficient. Theoretical class is intended to be a superclass. To utilize a theoretical class, you need to infer a subclass, abrogate and give execution to all the unadulterated virtual capacities. You would then be able to make examples from the solid subclass. C++ permits usage for unadulterated virtual capacity. For this situation, the =0 just make the class unique. As the outcome, you can't make examples. 3.4 Example: Shape and its Subclasses ClassDiagram_Shape.png [TODO] Description /* Implementation for Shape class (Shape.cpp) */ #include "Shape.h" #include <iostream> /Constructor Shape::Shape(const string and shading) { this->color = shading; } /Getter string Shape::getColor() const { bring shading back; } /Setter void Shape::setColor(const string and shading) { this->color = shading; } void Shape::print() const { std::cout << "State of color=" << shading; } /* Header for Circle (Circle.h) */ #ifndef CIRCLE_H #define CIRCLE_H #include "Shape.h" /The class Circle is a subclass of Shape class Circle : public Shape { private: int sweep;/Private information part public: Circle(int sweep = 1, const string and shading = "red");/Constructor int getRadius() const;/Getter void setRadius(int sweep);/Setter void print() const;/Override the virtual capacity twofold getArea() const;/to actualize virtual capacity }; #endif /* Implementation for Circle (Circle.cpp) */ #include "Circle.h" #include <iostream> #define PI 3.14159265 /Constructor Circle::Circle(int sweep, const string and shading) : Shape(color), radius(radius) { } /Getters int Circle::getRadius() const { bring sweep back; } /Setters void Circle::setRadius(int sweep) { this->radius = sweep; } void Circle::print() const { std::cout << "Circle radius=" << sweep << ", subclass of "; Shape::print(); } /Implement virtual capacity acquired for superclass Shape twofold Circle::getArea() const { return sweep * range * PI; } /* Header for Rectangle class (Rectangle.h) */ #ifndef RECTANGLE_H #define RECTANGLE_H #include "Shape.h" /The class Rectangle is a subclass of Shape class Rectangle : public Shape { private: int length; int width; public: Rectangle(int length = 1, int width = 1, const string and shading = "red"); int getLength() const; void setLength(int length); int getWidth() const; void setWidth(int width); void print() const;/Override the virtual capacity twofold getArea() const;/to execute virtual capacity }; #endif /* Implementation for Rectangle (Rectangle.cpp) */ #include "Rectangle.h" #include <iostream> /Constructor Rectangle::Rectangle(int length, int width, const string and shading) : Shape(color), length(length), width(width) { } /Getters int Rectangle::getLength() const { bring length back; } int Rectangle::getWidth() const { bring width back; } /Setters void Rectangle::setLength(int length) { this->length = length; } void Rectangle::setWidth(int width) { this->width = width; } void Rectangle::print() const { std::cout << "Square shape length=" << length << " width=" << width << ", subclass of "; Shape::print(); } /Implement virtual capacity acquired from superclass Shape twofold Rectangle::getArea() const { return length * width; } /* A test pilot program for polymorphism (TestShape.cpp) */ #include "Circle.h" #include "Rectangle.h" #include <iostream> utilizing namespace sexually transmitted disease; int fundamental() { /Circle object Circle c1(5, "blue"); c1.print(); cout << endl; cout << "area=" << c1.getArea() << endl; /Rectangle object Square shape r1(5, 6, "green"); r1.print(); cout << endl; cout << "area=" << r1.getArea() << endl; /Shape s1;/Cannot make example of dynamic class Shape /Polymorphism Shape * s1, * s2;/Shape pointers s1 = new Circle(6);/Dynamically dispense a subclass occasion s1->print();/Run subclass variant cout << endl; cout << "area=" << s1->getArea() << endl;/Run subclass variant of getArea() s2 = new Rectangle(7, 8);/Dynamically dispense a subclass occasion s2->print();/Run subclass variant cout << endl; cout << "area=" << s2->getArea() << endl;/Run subclass form of getArea() erase s1; erase s2; /Shape s3 = Circle(6);/mistake: can't assign an object of theoretical sort 'Shape' Circle c3(8); Shape and s3 = c3;/Object reference s3.print(); cout << endl; cout << "area=" << s3.getArea() << endl; Circle c4(9); Shape * s4 = &c4;/Object pointer s4->print(); cout << endl; cout << "area=" << s4->getArea() << endl; } Circle radius=5, subclass of Shape of color=blue area=78.5398 Square shape length=5 width=6, subclass of Shape of color=green area=30 Circle radius=6, subclass of Shape of color=red area=113.097 Square shape length=7 width=8, subclass of Shape of color=red area=56 Circle radius=8, subclass of Shape of color=red area=201.062 Circle radius=9, subclass of Shape of color=red area=254.469 [TODO] Explanation 3.5 Dynamic Binding versus Static Binding [TODO] 4. More On OOP 4.1 const Objects and const Member Functions
Visit: https://www.chlopadhe.com/constructor-overloading-in-c/ Consistent Object: We can utilize const to determine that an item isn't impermanent. For instance, const Point p1;/Constant article Point p2;/Non-steady article /p1 = p2;/blunder: const object can't be reassigned p2 = p1;/OK Consistent Member Functions: We pronounce a part work steady by setting the watchword const after the boundary list. A const part work can't change any part factor. For instance, int getX() const { return x; } void print() const { cout << "(" << x << "," << y << ")" << endl; } A consistent part work can't adjust information individuals as well. For instance, void setX(int x) const { this->x = x;/ERROR! } The constructor and destructor can't be made const, as they need to introduce information individuals. Notwithstanding, a const article can summon non-const constructor. The const property starts after development.
Comments
Post a Comment