Good day!

There is a task that needs to be solved through classes. I could not figure it out with them, with the creation of the .h file and in general. I decided how I could, because tomorrow I can pass on the DD, the code is working, but can I somehow fix it so that the described requirements for the classes are met? Task description may not be accurate (in translation).


α) Write a program that will use the Rectangular class with the rectangle data. The variables width and height contain the data of the sides of the rectangle and belong to the class.

Write α) constructor which takes no arguments β) constructor which takes 2 arguments γ) setWidth, setHeight which take two sides of the δ rectangle

Next, write code that works for two rectangles (10.7) and (10.12). The data of the first is set initially, the second is set by setWidth and setHeight. The result - the display of their area

β) Write square class Square, is an extension of the rectangle class. Write a constructor that will receive the value of the side of the square.

Further, the program should work for squares with sides 10 and 7. Also display their area and perimeter.

γ) Add a Box-cube class and a constructor that will receive the three cube parameters (length, height, width) getVolume and getArea are used from the parent class. scale (double f) multiplies the parameters (dimensions) of the cube by f. In the program - 2 cubes (10,10,10) and (7,8,3). The screen displays the volume of the cube, the area of ​​its surfaces. Then the scale is called - coefficients 0.5 and 2. New data is displayed on the screen.

Total: 1 program with 1 main, 3 .cpp and 3 .h (1 for each class (rectangle, squre, box))


and here is the code itself:

#include <iostream> #include <conio.h> #include <cstdlib> using namespace std; class percalc { public: void per(int a); void per(int a,int b); }; void percalc::per(int a) { cout<<"\n Perimetr of Square ="<<(4*a); } void percalc::per(int a, int b) { cout<<"\n Perimetr of Rectangle="<<((a+b)*2); } class areacalc { public: void area(int a); void area(int a,int b); void area(int a,int b,int d); }; void areacalc::area(int a) { cout<<"\n Area of Square="<<(a*a); } void areacalc::area(int a,int b) { cout<<"\n Area of Rectangle="<<(a*b); } void volume (int a,int b,int c) { cout<<"\n Volume of box="<<a*b*c; } void area (int a, int b, int c) { cout<<"\n Area of the first side of box="<<a*b; cout<<"\n Area of the second side of box="<<a*c; cout<<"\n Area of the third side of box="<<c*b; cout<<"\n Area of all sides of box="<<2*a*b+2*a*c+2*b*c; } void scale (int a,int b, int c, float sc) { area (a*sc, b*sc, c*sc); volume (a*sc, b*sc, c*sc); } main() { areacalc ac; percalc pc; cout<<"Rectangle with sides 10 and 7"<<endl; ac.area(10,7); pc.per (10,7); cout<<"\n\n\nRectangle with sides 10 and 12"<<endl; ac.area(10,12); pc.per (10,12); cout<<"\n\n\n\nSquare with side 10"<<endl; ac.area(10); pc.per (10); cout<<"\n\n\nSquare with side 7"<<endl; ac.area(7); pc.per (7); cout<<"\n\n\n\nCube with side 10"<<endl; area (10,10,10); volume(10,10,10); cout<<"\n\n\n\nScale 0,5"<<endl; scale (10,10,10,0.5); cout<<"\n\n\n\nScale 2"<<endl; scale (10,10,10,2); cout<<"\n\n\nBox with sides 7, 8 and 3"<<endl; area (7,8,3); volume(7,8,3); cout<<"\n\n\nScale 0,5"<<endl; scale (7,8,3,0.5); cout<<"\n\n\nScale 2"<<endl; scale (7,8,3,2); getch(); } 
  • Something tells me that this code of yours has nothing to do with this task, and you added it here, only to somehow bypass the forum rules: P - MDJHD
  • @Mary Ustyantseva: And what exactly does not like the compiler in header-files? Lay out the code and error message, and to which line it applies. - VladD

2 answers 2

Have you forgotten the Rectangular class? All the functions you have written may well be static, since they do not use private class fields, not to mention the fact that there are no fields at all.

For the future, classes in files are arranged like this:

 // some_type.h //===================== /* С помощью деректив препроцессора необходимо обезопаситься от повторного включения объявления класса. Например, когда type1.h инклудится в type2.h, а потом нам будет необходимо заинклудить type1 и type2 в одном файле (по тем или иным причинам). Выходит, что type1 будет включен дважды (явно и не явно через type2). */ #ifndef __SOME_TYPE_H__ #define __SOME_TYPE_H__ class SomeType { int memberVar_; // закрытое поле public: SomeType(int value); // Геттеры и сеттеры для доступа к закрытому полю int getValue() const { return memberVar_; } void setValue(int value) { memberVar_ = value; } void someMemberFunction(); }; #endif // __SOME_TYPE_H__ //===================== // some_type.cpp //===================== #include "some_type.h" SomeType::SomeType(int value) : memberVar_(value) { // ... тело конструктора } void SomeType::someMemberFunction() { //... тело метода } 

    Prototypes of classes, I think it’s easy to add the code of the f-th, if of course you wrote the code above yourself :))):

     // Rectangular.h class Rectangular { protected: double width; double height; public: Rectangular(); Rectangular(double height, double width); void setWidth(double width); void setHeight(double height); double getWidth(); double getHeight(); double getArea(); double getPerimeter(); }; // Square.h class Square: public Rectangular { public: Square(double side); }; // Box.h class Box: public Rectangular { protected: double depth; public: Box(double width, double height, double depth); double getVolume(); double getArea(); void scale(double f); } 
    • The code itself was written, but when creating classes in .h files, the compiler swears. I make a mistake somewhere and do not understand where :( - Mary Ustyantseva
    • Create somename.h, put the class definitions in it, put the implementations of the f-th in somename.cpp, at the beginning of which you should include somename.h and give, if possible, errors that the compiler produces - MDJHD