 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Concepts of classes and objects in C/C++ |
|
|
how could I write a program that will declare a class point. The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3 |
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
Just a note: There is no language "C/C++". We say that because, among the
C-style languages, C and C++ have very different usages and strategies.
Posters are advised to always get clear about which one they mean, before
posting, for best results!
ttique63 wrote:
| Quote: | how could I write a program that will declare a class point.
|
By writing a program with lots of X and Y variables, and lots of unit tests.
The refactor the program, passing all the tests after the fewest possible
edits, over and over again until all Xs and Ys have migrated into the exact
kind of Point class that this program needs.
No lie; that's the most sophisticated way to do it. If you start by guessing
your program needs such-and-so Point class, and if you write the class
first, you will burden your design with decisions made without feedback.
| Quote: | The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3
|
Oh, back up a minute. This is homework, right?
Read your tutorial, write your Point class first - exactly like your
professor told you to - and don't mention _anything_ I posted here.
Then when you have taken a crack at your Point class, post it here and we
will gleefully review it. Possibly with an eye towards keeping you out of
trouble, instead of in it.
Posting your raw homework assignment, as given, is kind'a tacky. ;-)
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!! |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
* attique63 (AT) hotmail (DOT) com:
| Quote: | how could I write a program that will declare a class point. The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3
|
Make a better effort of conceiling the homework nature of your question,
please.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
* Alf P. Steinbach:
| Quote: | * attique63 (AT) hotmail (DOT) com:
how could I write a program that will declare a class point. The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3
Make a better effort of conceiling the homework nature of your question,
please.
|
Then I promise to try at least once to make a better effort at speling.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sat Jul 15, 2006 6:42 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
wkaras (AT) yahoo (DOT) com wrote:
| Quote: | Daniel T. wrote:
...
First declare a class called Point.
Then give it two private data members x and y of type float.
...
It's debatable whether the x and y members should be private or public.
There is no combination of values of x and y that would result
in an object of class Point having an invalid state. So there is
no strict need for the x and y data member to be private. If
you find:
p += Point(0.5, 0.0);
to be more aesthetically pleasing than:
p.x += 0.5;
then it might make sense to make x and y private. But I don't
immediately see any other reason. Unless you're a SmallTalk
programmer wannabe who thinks it's self-evident that all
data members have an inalienable right to be private .
|
All that is fine if you assume that the class will always and forever,
only be implemented using two variables held in RAM representing a
cartesion coordinate system on this particular machine and in this
particular thread and no code will *ever* have to patch into objects of
the class to detect changes to one or both variables.
Now all that may very well be true especially for such a tiny little
class, however the OP's *requirement* was for the class to have "two
private data members x and y of type float". Personally, I was not in
the habit of leaving my homework requirements unfulfilled and I see no
reason to suggest others do so. |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sat Jul 15, 2006 6:49 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
Frederick Gotham wrote:
| Quote: | Actually, I've decided to adopt a sadistic approach toward those who post
here blatantly demanding that their homework be done.
I posted in the heartfelt hope that the OP would copy-paste the code directly
and submit it to their teacher/lecturer/mentor/etc.
I would request, in the spirit of sadism, that you don't correct (or point
out the errors in) my posts which are a reply to people asking for their
homework to be done.
This is only my second time affecting this approach, but it's fun so far!
|
I like it, except can I suggest a change? Make your code completely
correct and compilable, but so obtuse/obfuscated that the OP's teacher
will know at a glance that there is no way that this student of his
created it. Just as fun as posting wrong code, but more challanging for
you. |
|
| Back to top |
|
 |
mo Guest
|
Posted: Sat Jul 15, 2006 9:10 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
On 13 Jul 2006 22:47:50 -0700, attique63 (AT) hotmail (DOT) com wrote:
| Quote: | how could I write a program that will declare a class point. The class
point has two private data members x and y of type float. The class
point has a parameterized constructor to initialize both the data
members i.e. x and y. The class point has a member function display()
that display the value of x and y. Create two objects p1 and p2 with
your desired data and display the values of x and y of p1 and p2. Now
create a third object p3 by using the expression p3=p1+p2 and display
the values of x and y of p3
|
If you can get the class declaration and implementation written, this
should do the trick for the rest!
#include <iostream>
#include <ifstream>
#include <cstdio>
#include <string>
#include <locale>
#include <iterator>
#include <cmath>
#include <pointclass.h> //Create this class def and you're good to go!
#include <fcntl.h>
int main() {
goto decBlock;
subBlockA:
slgdb = true;
pointarray[++i]=Point2;
if (aPx2z) goto superBlockB;
goto decBlock;
dispBlock:
std::locale euroLoc(std::locale("American_USA.1252");
std::cout.imbue(euroLoc);
if (slgdb)
goto addBlock;
goto subBlockA;
decBlock:
Point Point1();
Point Point2(32.1233,-99121243.551);
int i=12;
int j=44;
bool slgdb = false;
bool aPx2z = true;
Point pointarray[9520];
goto dispBlock;
addBlock:
while (j < 612) {
j = (j*2)-34;
j--;
}
pointarray[j] = Point1.add(Point2.getresult());
for (int k = 9520; k > 0; --k) {
if pointarray[k].equatesTo(Point1.add(Point2.getresult())) {
Point point3(pointarray[k].getXvalue,pointarray[k].getYvalue);
}
}
FILE *fs;
point3.display(fs);
return -9991;
superBlockB:
i = 4322;
pointarray[--i] = Point1;
goto dispBlock;
} |
|
| Back to top |
|
 |
Robbie Hatley Guest
|
Posted: Sat Jul 15, 2006 9:10 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
"Frederick Gotham" wrote:
| Quote: | Robbie Hatley posted:
1. There's some errors in there. I'm assuming you put those
in there to force the OP to research the correct syntax for
himself, so I won't say what they are.
Actually, I've decided to adopt a sadistic approach toward those who post
here blatantly demanding that their homework be done.
I posted in the heartfelt hope that the OP would copy-paste the code directly
and submit it to their teacher/lecturer/mentor/etc.
I would request, in the spirit of sadism, that you don't correct (or point
out the errors in) my posts which are a reply to people asking for their
homework to be done.
|
Yikes!
Oh, wow. Deja vu so strong my head is litterally spinning.
I was walking to the street yesterday to a Circle K to buy milk,
when it occurred to me, "Perhaps Frederick Gotham put those errors
in that program so that the student would get in trouble with his
teacher when he handed it in as his own work? That would be pretty
sadistic; but then, cheating is a pretty horrible thing to do. I
hope he doesn't get miffed because I mentioned the fact that there
were errors in the program? But perhaps the lazy student will grab
the work and turn it in before he reads my post."
Honestly, I thought all those things!
I'll keep my mouth shut about hidden monkey wrenches from now on.
:-)
Ewwwww! I love the cheery "always love to help out a fellow student!"
Now here's a program that rotates ASCII characters numerically
clockwise by 13. (Not quite a ROT13, but similar concept.)
Except, it doesn't. Even if you get rid of all the compile-time
bugs (and there's lots) it'll crash at runtime. Cheating-student
fodder, perhaps:
int Main(Void)
{
chair Bob[42] = "What in Sam Hill???"
for (i = 0; i<500; +i);
{
if (Bob[i] > 127);
{
Bob[i] =- 128;
}
Bob[i] = ((Bob[i] + 13)%128);
}
if (NUL != Bob);
{
pprintf("String is: %S, Bob);
}
Return 73;
};
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/ |
|
| Back to top |
|
 |
Guest
|
Posted: Sun Jul 16, 2006 2:36 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
Daniel T. wrote:
| Quote: | wkaras (AT) yahoo (DOT) com wrote:
Daniel T. wrote:
...
First declare a class called Point.
Then give it two private data members x and y of type float.
...
It's debatable whether the x and y members should be private or public.
There is no combination of values of x and y that would result
in an object of class Point having an invalid state. So there is
no strict need for the x and y data member to be private. If
you find:
p += Point(0.5, 0.0);
to be more aesthetically pleasing than:
p.x += 0.5;
then it might make sense to make x and y private. But I don't
immediately see any other reason. Unless you're a SmallTalk
programmer wannabe who thinks it's self-evident that all
data members have an inalienable right to be private .
All that is fine if you assume that the class will always and forever,
only be implemented using two variables held in RAM representing a
cartesion coordinate system on this particular machine and in this
particular thread and no code will *ever* have to patch into objects of
the class to detect changes to one or both variables.
|
If you google for "Bjarne Stroustrup", you can find and read
his arguments as to why it's not a good idea to design
classes to allow for every conceivable future use of the
class.
I think it's part of the class interface that the points are
in a cartesian coord system. But your argument for always
making data member private because it allows for future
access "side effects" does have some merit.
| Quote: |
Now all that may very well be true especially for such a tiny little
class, however the OP's *requirement* was for the class to have "two
private data members x and y of type float". Personally, I was not in
the habit of leaving my homework requirements unfulfilled and I see no
reason to suggest others do so.
|
In these dark times, it's good to see that some people still
follow strict ethical standards when helping others to cheat on their
homework. |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sun Jul 16, 2006 3:20 am Post subject: Re: Concepts of classes and objects in C/C++ |
|
|
In article <1152999413.624409.125720 (AT) 35g2000cwc (DOT) googlegroups.com>,
wkaras (AT) yahoo (DOT) com wrote:
| Quote: | Now all that may very well be true especially for such a tiny little
class, however the OP's *requirement* was for the class to have "two
private data members x and y of type float". Personally, I was not in
the habit of leaving my homework requirements unfulfilled and I see no
reason to suggest others do so.
In these dark times, it's good to see that some people still
follow strict ethical standards when helping others to cheat on their
homework.
|
Helping others cheat? You might want to look at the OP and my response
again... I in no way helped anyone to cheat. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|