Concepts of classes and objects in C/C++

A

attique63

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
P

Phlip

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!
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.
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. ;-)
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* (e-mail address removed):

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.
 
R

Robbie Hatley

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

Ummm... by reading about classes in your textbook, then putting
what you learn into practice? In the time you spent typing the
message quoted above, you could have been done with your homework
assignment already.

I'll give you one hint to get you going: declaring a class called
"Point" is going to involve writing "class Point". The syntax is
in your textbook. Read about "classes" there, please.

Once you've written your program, if it doesn't work, come back
here and copy-and-paste your program and ask for help. You'll find
that folks here are willing to help you with your homework. NOT do
it for you, though! HELP you. That means, you have to do most of
the work yourself. Get cracking.


--
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/
 
T

tragomaskhalos

Another tip when trying to get people to do your homework for you - try
to avoid using what is obviously the course module title as the subject
line !
 
F

Frederick Gotham

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

class Point : public {

private float x, y;

public Point(float,float) : { x(arg1), x(arg2) }

public void Display() volatile
{
puts(FLOATING,x,y);
}

};


void main(int)
{
Point p1 = { 5.6,4.3 };
Point p2 = { 2.1,8.9 };

p1->Display(void);
p2->Display(void);

Point p3 = p1 + p2;

p3->Display(void);
}
 
R

Robbie Hatley

Frederick Gotham said:
class Point : public {

private float x, y;

public Point(float,float) : { x(arg1), x(arg2) }

public void Display() volatile
{
puts(FLOATING,x,y);
}

};


void main(int)
{
Point p1 = { 5.6,4.3 };
Point p2 = { 2.1,8.9 };

p1->Display(void);
p2->Display(void);

Point p3 = p1 + p2;

p3->Display(void);
}

Tut-tut, my dear fellow; if you're going to do the OP's homework
for him, you should at least get him to slip you a ten spot. :)

Two things I notice, though:

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.
2. The subject line is all wrong, isn't it? Classes in C? No
such thing in C. Should read: "Concepts of classes and objects
in C++" People sure do like talking about that non-existant
language C/C++. (Actually, C/C++ would always be 1, regardless
of what's in C. But I digress.)

--
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/
 
D

Daniel T.

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

First declare a class called Point.

Then give it two private data members x and y of type float.

Then write a parameterized constructor that initializes both data
members (i.e. x and y.)

Then write a member function called "display()" that displays the value
of x and y.

Now (this is the tricky part) create a non-member function operator+
that accepts two Points as parameters and returns a Point. Have it add
the x's and y's of the two points passed in.

After you do all of the above, create two objects p1 and p2 with some
data (whatever you desire) and call display on them.

Then create a third object p3 by using the expression p3 = p1 + p2 and
call display on it.
 
S

shadowman615

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

Did you even bother trying to figure any of this out? This reads as if
you simply copied your homework assignment instructions verbatim. It
couldn't be more clear what you have to do here -- you have precise
instructions about how to design your program, and what steps to
follow.

Why don't you take a stab at this and come back if you get stuck. But
don't post any more questions until you have some code written. Many
people here would be happy to help, but you need to do some of the
work.
 
N

Noah Roberts

Robbie said:
2. The subject line is all wrong, isn't it? Classes in C? No
such thing in C. Should read: "Concepts of classes and objects
in C++" People sure do like talking about that non-existant
language C/C++. (Actually, C/C++ would always be 1, regardless
of what's in C. But I digress.)

Well, if it is "concepts of...," then there is such a thing in C.
There is no construct "class" in the C language but you most certainly
can make them so there is such a concept.

And I would debate that there is no such language as C/C++. Perhaps
not formally defined but certainly in widespread use. Any time you see
char* really you are working in C/C++.
 
V

Victor Bazarov

Noah said:
[..]
And I would debate that there is no such language as C/C++. Perhaps
not formally defined but certainly in widespread use. Any time you
see char* really you are working in C/C++.

OK, let's debate. When you see 'if' what language do you work in?

V
 
F

Frederick Gotham

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.

This is only my second time affecting this approach, but it's fun so far!

Here's my first one:

http://groups.google.ie/group/comp.lang.c/msg/14dc37e49bd6522d?hl=en&
 
V

Victor Bazarov

Frederick said:
[...] 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.

[...]

On a larger scale your approach accomplishes nothing, except that
unsuspecting newcomers who see your replies will think that their
questions do get answered with code and that means we'll get more
traffic asking for homework help. So, not only it doesn't help to
ward off potential do-my-homework-for-me types, it encourages more
noise in the newsgroup. As I see it, your approach is, well, bad.

V
 
N

Noah Roberts

Victor said:
Frederick said:
[...] 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.

[...]

On a larger scale your approach accomplishes nothing, except that
unsuspecting newcomers who see your replies will think that their
questions do get answered with code and that means we'll get more
traffic asking for homework help.

And when those dumbasses flunk we won't have to work with them later.
 
W

wkaras

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 :) .
 
D

Daniel T.

Daniel T. wrote:
...
...

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.
 
D

Daniel T.

Frederick said:
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.
 
M

mo

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;

}
 
R

Robbie Hatley

Frederick Gotham said:
Robbie Hatley posted:




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.
:)
This is only my second time affecting this approach, but it's fun so far!

Here's my first one:

http://groups.google.ie/group/comp.lang.c/msg/14dc37e49bd6522d?hl=en&

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 > 127);
{
Bob =- 128;
}
Bob = ((Bob + 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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top