c++ - Rectangle class -
hi im new c++ , may in on head on problem im trying solve. visual explanation , solution errors or better revised source code ask of. invest there interest question.
heres problem: design class named rectangle represent rectangle. class must contain:
- two double data fields named width , height specify width , height of rectangle.
- a no-arg constructor creates default rectangle width 1 , height 1.
- a constructor creates rectangle specified width , height
- the accessor , mutator functions data fields
- the function named area() returns area of rectangle
- a function named getperimeter() returns peremter.
draw uml diagram class. implement class. write test progranm creates 2 rectangle obejects. assign width 4 , height 40 first object , width 3.5 , height 35.9 second. display properties of both objects , find areas , perimeters.
heres have far:
#include <iostream> using namespace std; class rectangle { public: double height; public: double width; rectangle() { width = 4; } rectangle(double newarea) double height; height() ( height = 40 { { area = height* width; } double getarea() { return area; } bool ison() { return on; } double getperimeter() { return perimeter; } void setperimeter(double radius) cout << "the area of rectangle" << rectangle1.area<<"is"<<rectangle1.getarea()<< endl; cout<<"the area of rectangle" <<rectangle.area2.area<<"is"<<rectangle2.getarea()<<endl; return 0; }
if you'd learn whats going on here, check out
#include <iostream> using namespace std; class rectangle{ private: // in cases want keep member variables private double height; // allows of how accessed. double width; // provides level of security class. public: // need 1 public: // constructors called when define new variable of type rectangle rectangle() // no arguments means constructor takes no input when called. { width = 1.0; // sets width , height 1 height = 1.0; } rectangle(double a_width, double a_height) { // constructor needs exact(case-sensitive) match of class-name /* set width , hieght arguments passed constructor in here.*/ } // accessor / mutator methods more called get/set methods. double getheight(){ // height called on rectangle class return height; // returns value of class member "height" } /*make method width here.*/ // in c++ can return results of equations directly // i.e. // int x = 2; // return x*x; // returns 4 // try simliar getarea() , getperimeter(); double getarea() const { return /*area equation goes here*/ ; } double getperimeter() const { return /*perimeter equation goes here*/; } } // should split these displaying statements main function, // meant do, right? ;) int main(){ // in order use new class, need declare variable of type rectangle rectangle1; // when declare new variable without arguments default constructor called. // means rectangle class decides use height = 1; width = 1; constructor above. // can check using getheight() , getwidth(), when implement them. cout << "the area of rectangle is" << rectangle1.getarea() << endl; return 0; }
ah reminds me of taing csci1100
Comments
Post a Comment