Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I make use of the class here?
I found that the code would be too long if I wanted to add more features such as perimeter in addition to the area.
I'm new to C++, not even a week.
I wanted to use class to simplify the code, unsuccessful.
This is the original code without class.
C++
#include <iostream>
using namespace std;

void rectangle()
{
    float height = 0;
    float width = 0;

    cout<<"Rectangle"<<endl;
    cout << "Enter the height:" << endl;
    cin >> height;

    cout << "Enter the width:" << endl;
    cin >> width;

    int area = height * width;
    cout << "The area is " << area << "." << endl;
    
    main();
}

/* More shape functions */

int main()
{
	
	cout << "Choose Shape:" << endl;
	cout << "1. Rectangle" << endl;
        /* More shapes */
	cout << "8. Sector" << endl;
	cout << "0. QUIT" << endl;

    bool retry = false;
	while (retry)
	{
		char user_input = '0';
		cin >> user_input;
		switch (user_input)
		{
		case '1':
			rect_area();
			break;
		case '2':
			tri_area();
			break;
		/* more */
		case'8':
			sect_area();
			break;
		case'9':
			retry = true;
		default:
			exit(0);
		}
	}
}


I've tried using class, but unsuccessful.
C#
class area
{
	public:
		void shape_height(float h) {
			float height = h;
		}

		float get_height() {
			return height;
		}
                void shape_width(float w) {
                       float width = w;
                }

                float get_width() {
                     return width;
                }
	private:
		float height;
		float width;
}

int main()
{
	area measure;
	measure.shape_height(12); // How to make user input their data here?

}


What to put in the main() to call the class and it's function?
Posted

I suggest you to check out a good C++ tutorial (or, better, a book). You cannot write C++ code if you really have no idea about.
You may find a good starting point here: "C++ Language"[^].
 
Share this answer
 
Comments
Maciej Los 24-Aug-15 13:53pm    
Short and to the point!
5ed!
CPallini 24-Aug-15 14:08pm    
Thank you, Maciej.
Sergey Alexandrovich Kryukov 24-Aug-15 14:44pm    
Fair enough. 5ed, but there is really more to it. Please see Solution 2.
—SA
I want to note, the purpose of classes and other fundamentally important language and technology features is not exactly "simplifying" anything. They create fundamentally new approaches. Even though the final result can be compared with some non-OOP solution; then, the OOP solution can look much simpler and maintainable. However, this is not the point. The real point it to solve much more complex problems as yours in much more efficient and maintainable manner. In other words, the point is avoiding poor solutions at all, so you would not have a material for comparison in "simplicity" (which is, by the way, a very fuzzy notion, to discuss it seriously here).

A language that doesn't affect the way you think about programming, is not worth knowing.
Ultimately, this is perfectly true for OOP. It gives you principally new mechanism, first of all, dynamic dispatch based on the mechanism of virtual methods, and, thanks to it, late binding and polymorphism. Please see:
https://en.wikipedia.org/wiki/Dynamic_dispatch[^],
https://en.wikipedia.org/wiki/Virtual_function[^],
https://en.wikipedia.org/wiki/Late_binding[^],
https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

If you don't use it, your "classes" could be almost pointless, some way of code structuring at best. There are so many developers who use many classes (and sometime event virtual functions) without understanding of their true purpose and even the trace of OOP. Everything without dynamic dispatch mechanism (including encapsulation and inheritance) is just prerequisite for real OOP. Using classes just for sake of using classes (as well of some other language features) is one of the common mistakes.

Now, I would like to bring your attention to what I think is the worst in your code. This is immediate constants 1, 2, 8 and 9 hard coded in lines like case 9: this is not maintainable. You could at least use some enum declaration.

And every developers needs to read this, first of all:
Peter Norvig, Teach Yourself Programming in Ten Years,
http://norvig.com/21-days.html[^].

—SA
 
Share this answer
 
Comments
CPallini 24-Aug-15 15:34pm    
5.Ouch, overkill :-)
Sergey Alexandrovich Kryukov 24-Aug-15 15:38pm    
:-)
Thank you, Carlo.
—SA
Maciej Los 24-Aug-15 17:00pm    
Well... Excellent answer, 5ed!
I had read a title at the bottom of your answer in disbelief. 10 years? I thought i see: 10 hours, 10 minutes, but not 10 years. Excellent article!
Sergey Alexandrovich Kryukov 24-Aug-15 18:10pm    
Thank you, Maciej.
Yes, the article is really goes against the "trend", it's 10 years. There is so much to it. Many people told me that really entering their field of activity should take some 10 years. It's about everything: sport, field of physics and mathematics, and so on.
—SA
Maciej Los 25-Aug-15 1:45am    
Completely agree!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900