-
April 22nd, 2017, 03:20 PM
#1
[RESOLVED] I need help with writing a program in C++ (I'm still a newbie)
Define class for geometric shape triangle. The member variables of the class are the lenghts of the sides of the 3 sides of the triangle. The member functions have to include: constructor that checks if the entered values are sides of a triangle, destructor, fuction for calculating and output the face of the triangle. To create a main function which creates an object from the class and tests the constructor and the destructor.
( The second sentence doesn't make much sense though, because my English is broken .Thanks in advance )
-
April 22nd, 2017, 04:27 PM
#2
Re: I need help with writing a program in C++ (I'm still a newbie)
Well where is exactly your problem?
Can't you create a class? or just a particular class?
Or something else?
Victor Nijegorodov
-
April 23rd, 2017, 03:26 AM
#3
Re: I need help with writing a program in C++ (I'm still a newbie)
Do you know how to check that given 3 sides they make a triangle?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 23rd, 2017, 07:43 AM
#4
Re: I need help with writing a program in C++ (I'm still a newbie)
I can create the class. But the problem is that I really have no idea what to do with those constructors and destructors. I know how to declare them, but that's all I know. No idea what to type in them.
-
April 23rd, 2017, 07:53 AM
#5
Re: I need help with writing a program in C++ (I'm still a newbie)
Do you know geometry? How to check whether the triangle with the given lenghts of the sides exists?
Do you know how to calculate the face of the triangle?
If not - then start with the wiki: https://en.wikipedia.org/wiki/Triangle
Victor Nijegorodov
-
April 23rd, 2017, 08:07 AM
#6
Re: I need help with writing a program in C++ (I'm still a newbie)
I do know that. Just asked if somebody could help me, because I've been studying C++ for 4 days and have no idea what to write in those constructors. With those public and private things. Searched in many websites and still didn't get it. I'll figure it out myself somehow. Thanks for the replies though...
-
April 23rd, 2017, 08:19 AM
#7
Re: I need help with writing a program in C++ (I'm still a newbie)
Well, the constructor is just creates the class object. in your case it means you should initialize the class members (lengths of the sides).
I'd provide the default ctor that just sets all the lengths to zero, and ctor with three integer parameters being the lengths of the sides.
Then you have to implement the method of your class that tests the lengths of the sides for the validity for a triangle.
If your problem is "public and private things" of the class then in the first step you could declare all of them as public...
Victor Nijegorodov
-
April 23rd, 2017, 08:38 AM
#8
Re: I need help with writing a program in C++ (I'm still a newbie)
I've been studying C++ for 4 days
How are you learning c++? From a book, class?
This site may be of interest
http://www.learncpp.com/
In this case you don't need to specify a destructor. Private class variables can only be accessed by class functions - not outside of the class. Public class variables can be accessed both from class functions and also from outside of the class. Usually member variables are private and class functions are provided to obtain their values and to set them as needed. Note that for class, if public: is not specified then all class members are private by default.
As you are dealing with a triangle, then you'll need to hold the values of the 3 sides in class member variables. These are set in the class constructor(s). The default constructor should initialise them to a known value (consider 0). The other constructor takes 3 arguments for the values of the 3 sides of the triangle.
Last edited by 2kaud; April 23rd, 2017 at 08:40 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 23rd, 2017, 10:27 AM
#9
Re: I need help with writing a program in C++ (I'm still a newbie)
So far I've come up with this (I know it might be completely wrong):
Code:
#include <iostream>
#include <math.h>
using namespace std;
class Triangle
{
public:
int a,b,c;
Triangle();
~Triangle();
};
Triangle::Triangle ()
{
cin >> a >> b >> c;
if (a+b > c && a+c > b && b+c > a)
cout << "The sides form a triangle" << endl;
else
cout << "The sides do not form a triangle." << endl;
}
Triangle::~Triangle ()
{
}
int face()
{
cin >>a >>b >> c;
p=(a+b+c)/2;
S=sqrt(p*(p-a)*(p-b)*(p-c);
cout << "The face of the triangle is: " << S <<endl;
}
int main()
{
return 0;
}
So... I have no idea what to do with this destructor. Don't know what to type in the main function (it should create a new object from the class and tests the constructor and the destructor). Also not sure if I should type "cin" again in "face" function, since I already did in the constructor.
Help me please.
Last edited by 2kaud; April 23rd, 2017 at 10:44 AM.
Reason: Formatted code
-
April 23rd, 2017, 10:33 AM
#10
Re: I need help with writing a program in C++ (I'm still a newbie)
Please, use CODE tags ( [ code ] ... [ /code ] without the spaces) around code snippets. Also, please, remove the line numbers.
Victor Nijegorodov
-
April 23rd, 2017, 10:51 AM
#11
Re: I need help with writing a program in C++ (I'm still a newbie)
I wouldn't have the constructor asking for input the sides. I would have them passed as constructor arguments and store them in the class variables - and obtain them in main(). Similarly for face(), you don't need to again obtain values but use the class stored values.
PS in face() you have used the variables p and s without first defining them.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 23rd, 2017, 10:53 AM
#12
Re: I need help with writing a program in C++ (I'm still a newbie)
Okay, sorry about that. I'm new in this website. So, what about the code? I know it is completely wrong, but need some help.
-
April 23rd, 2017, 10:59 AM
#13
Re: I need help with writing a program in C++ (I'm still a newbie)
Originally Posted by lebro98
Okay, sorry about that. I'm new in this website. So, what about the code? I know it is completely wrong, but need some help.
Ok I formatted your code as posted. No, your code is not completely wrong. As I mentioned in post #8, you don't need a destructor. Also face() should be a member function of the class not a standalone function so that it can used the saved values in the member variables.
Last edited by 2kaud; April 23rd, 2017 at 11:09 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 23rd, 2017, 11:09 AM
#14
Re: I need help with writing a program in C++ (I'm still a newbie)
Thanks for all the replies! It really helped me. But last thing and I won't bother you anymore, may somebody please tell me what to do in the main() function, because I really have no idea.
-
April 23rd, 2017, 11:09 AM
#15
Re: I need help with writing a program in C++ (I'm still a newbie)
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|