import std.stdio; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; } // Derived class class Rectangle: Shape { public: int getArea() { return (width * height); } } class Square: Rectangle { this(int side) { this.setWidth(side); this.setHeight(side); } } void main() { Square square = new Square(13); // Print the area of the object. writeln("Total area: ", square.getArea()); }
import std.stdio; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; } // Derived class class Rectangle: Shape { public: int getArea() { return (width * height); } } void main() { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. writeln("Total area: ", Rect.getArea()); }
import std.stdio; class Box { public: static int objectCount = 0; // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); length = l; breadth = b; height = h; // Increase every time object is created objectCount++; } double Volume() { return length * breadth * height; } static int getCount() { return objectCount; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; void main() { // Print total number of objects before creating object. writeln("Inital Stage Count: ",Box.getCount()); Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects after creating object. writeln("Final Stage Count: ",Box.getCount()); }
import std.stdio; class Box { public: static int objectCount = 0; // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); length = l; breadth = b; height = h; // Increase every time object is created objectCount++; } double Volume() { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; void main() { Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects. writeln("Total objects: ",Box.objectCount); }
import std.stdio; class Box { public: // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box } void main() { Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 Box *ptrBox; // Declare pointer to a class. // Save the address of first object ptrBox = &Box1; // Now try to access a member using member access operator writeln("Volume of Box1: ",ptrBox.Volume()); // Save the address of first object ptrBox = &Box2; // Now try to access a member using member access operator writeln("Volume of Box2: ", ptrBox.Volume()); }
import std.stdio; class Box { public: // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; } int compare(Box box) { return this.Volume() > box.Volume(); } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box } void main() { Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 if(Box1.compare(Box2)) { writeln("Box2 is smaller than Box1"); } else { writeln("Box2 is equal to or larger than Box1"); } }
import std.stdio; class Line { public: this() { writeln("Object is being created"); } ~this() { writeln("Object is being deleted"); } void setLength( double len ) { length = len; } double getLength() { return length; } private: double length; } // Main function for the program void main( ) { Line line = new Line(); // set line length line.setLength(6.0); writeln("Length of line : ", line.getLength()); }
import std.stdio; class Line { public: void setLength( double len ) { length = len; } double getLength() { return length; } this( double len) { writeln("Object is being created, length = " , len ); length = len; } private: double length; } // Main function for the program void main( ) { Line line = new Line(10.0); // get initially set length. writeln("Length of line : ",line.getLength()); // set line length again line.setLength(6.0); writeln("Length of line : ", line.getLength()); }
import std.stdio; class Line { public: void setLength( double len ) { length = len; } double getLength() { return length; } this() { writeln("Object is being created"); } private: double length; } void main( ) { Line line = new Line(); // set line length line.setLength(6.0); writeln("Length of line : " , line.getLength()); }
import std.stdio; class Line { public: double length; double getLength() { return length ; } void setLength( double len ) { length = len; } } void main( ) { Line line = new Line(); // set line length line.setLength(6.0); writeln("Length of line : ", line.getLength()); // set line length without member function line.length = 10.0; // OK: because length is public writeln("Length of line : ", line.length); }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more