Need Help regarding C++ Prog

This is a discussion on Need Help regarding C++ Prog within the C++ Programming forums, part of the General Programming Boards category; How to write a program that will declare a class point. The class point has two private data members x ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    Need Help regarding C++ Prog

    How to 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.
    Sample Output
    Point 1 is
    x=2.5
    y=3.8
    Point 2 is
    x=5.9
    y=6.8
    Point 3 is
    x=8.4
    y=10.6

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You will start by declaring the class point, adding 2 float data members who are private. The make a constructor that will initialize the afformentioned data members. Then create a function that is a member of class point called display and have it display the values of the data members mentioned above. Create 2 objects, I guess we could call them p1 and p2, and display the values of them. Then create another object, we can call it p3 and use p3=p1+p2. Then display the values of p3...


    http://cboard.cprogramming.com/annou...ouncementid=39

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Welcome to the forums, mrsharique.

    If you have a question, or if you don't know how to get started, we can help you. But, don't expect anyone here to write the whole progrm for you or to do your homework for you. Try to ask a specific question. Don't just repeat the assignment.

    If it looks like homework, you are more likely to get hints than a direct answer. Besides, programers are curious types who like to solve puzzles... It takes away all the fun if someone does the work for you!

    Write some code... show what you have so far, and tell us where you are "stuck". We can help you to move forward to the next step. If you get stuck again, ask another question.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Well, once you've gotten your class set up, and working fine, you'll have to overload some operators (the + one and the + one I think), to accomplish this:

    p3=p1+p2;

    There has been a lot of that of the forums recently, operator overloading I mean. Search the forums for examples of overloading operators when the class is set up.

    Yeah, this does seem very homework-y. So no code from twomers. Learning through blood and toil teaches well, in my opinion. Chuck 'em in the deep end and whatnot.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    12
    The only problem you're having is in the damn language that your programming homework booklet uses. Stuff like "class point" and "parameterized constructor (OH NOES!) is scaring you off, when it's simple as dirt.

    Lemme rewrite the specification for you, even though that's been done once already, it wasn't associated with this problem:

    Quote Originally Posted by mrsharique
    Write a program that will declare a class. The class has two private variables of type float. The class has a constructor to initialize both the variables. The class has a function that displays the value of x and of y to the screen.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Mmm, perhaps I should adopt Frederick's approach.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    I can do that without an effort.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> I can do that without an effort.

    What? Write buggy code Only joking. I like his approach though.

    (Edit: I should really stop insulting people. Sorry.)

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,579
    Well that was my point exactly. Writing buggy code
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 02:35 PM
  2. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  3. an option at the end of this prog to ask if I want to run again
    By bandito9111 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 02:30 PM
  4. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 08:43 AM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21