Well we started classes last week in my C++ class but I have had swine flu and missed a lot of it. Anyways heres my assignment:
a. Construct a class named Rectangl that has double-precision data members named length and width. The class should have member functions named perimeter() and area() to calculate the rectangle's perimeter and area, a member function named setdata() to set a rectangle's length and width, and a member function named showdata() that displays a rectangles length, width, perimeter, and area.
Ive been attempting this for about 3 hours now and am completely lost. I can't even get my code to compile and Im sure if I did it wouldn't work. Can anyone help me?
which is complete gibberish. It keeps going but this forum only lets 9000 chars so I didnt copy all of it.
Also im really not to sure how to rewrite my perimeter and area functions. I know the formulas for calculating them, but I missed a lot on functions too. Im trying to go by our book, but it leaves a lot out.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
class Rectangle
{
private:
double length;
double width;
public:
double perimeter();
double area();
void setdata(double, double);
void showdata();
};
double Rectangle::perimeter()
{
return ((2 * length) + (2 * width));
}
double Rectangle::area()
{
return length * width;
}
void Rectangle::setdata(double xx, double yy)
{
length = xx;
width = yy;
}
void Rectangle::showdata()
{
std::cout << "The length of the rectangle is "
<< length << " units" << std::endl;
std::cout << "The width of the rectangle is "
<< width << " units" << std::endl;
std::cout << "The perimeter of the rectangle is "
<< perimeter() << " units" << std::endl;
std::cout << "The area of the rectangle is "
<< area() << " sq. units" << std::endl;
}
int main()
{
double x;
double y;
std::cout << "What is the length of the rectangle: ";
cin >> x;
std::cout << "\nWhat is the width of the rectangle: ";
cin >> y;
std::cout << std::endl;
Rectangle a;
a.setdata(x,y);
a.showdata();
return 0;
}
And can anyone now explain to me what classes are used for? I know they're like one of the most important things about C++ and I don't want to miss anything.
The function of every computer program ever written, at a high level, is to take in input, process it, and
generate output. Your assignment explains what input your program is to take in, what it should do with
it, and what output it should generate.
You should be able to run your program over a representative set of inputs and manually verify
whether or not your program generates the correct output. Read: test it. You know what it is supposed
to do. Does it do what you want it to do?
So would this be correct? I added the default constructor but im getting a terminal error when I run it. It asks for how many students and after the input it crashes:
Your constructor is screwed up, you cannot assign arrays to each other like that. What you are doing is only assigning the 5 element of each array (which is out of bounds, btw) to each other.
It seems your problem lies within not knowing how arrays/variables are working...I suggest re-reading those sections of your textbook or this tutorial.
I missed a lot in class and it really sucks because the only thing I didn't learn on my own before the class was Classes, Structures, Arrays, and Functions (most notibaly passing values to functions ect). Turns out thats what was covered when I was absent and I know they're all extremely important to C++.
Did you try using a debugger to step through the program to see where the error occurs? That will often help you with any problems, not just this kind.