Classes Exercise (again)

This is a discussion on Classes Exercise (again) within the C++ Programming forums, part of the General Programming Boards category; Ok.. yet another homework.. hope I'm not bugging you guys out too much. "Make a program that processes computer orders. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Classes Exercise (again)

    Ok.. yet another homework.. hope I'm not bugging you guys out too much.

    "Make a program that processes computer orders. There are two ways computers can be ordered. Either in 3 packages (A, B, C) or custom configured computers. The custom computers can be modified in 3 ways: (CPU Speed, Hard Drive, RAM)

    Package A: 75 (Mhz) 1(GB) 16(MB of RAM)
    Package B: 100 (Mhz) 1.5(GB) 16(MB of RAM)
    Package C: 133 (Mhz) 2(GB) 32(MB of RAM)

    "

    And heres my program:

    Code:
    #include<iostream.h>
    
    
    
               
       class Computer
               
       {
       
       public:
                  
          void display(char a)
                  
          {
             if(a=='A')
             {
                cpuSpeed=75;
                hardDrive=1;
                memory=16;
                cout<<"A "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
             }
             if(a=='B')
             {
                cpuSpeed=100;
                hardDrive=1.5;
                memory=16;
                cout<<"B "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
             }
             if(a=='C')
             {
                cpuSpeed=133;
                hardDrive=2;
                memory=32;
                cout<<"C "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
             }
          }
                  
          void display(int b,double c,int d)
                  
          {
             cpuSpeed=b;
             hardDrive=c;
             memory=d;
             cout<<"Custom "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
          }
       
       
       
       private:
          int cpuSpeed, memory;
          double hardDrive;
       
       
       };
    
               
       int main()
               
       {
          Computer compaq(50,0.5,8), toshiba('A'), gateway('C');
          compaq.show();
          toshiba.show();
          gateway.show();
          return ;
       }
    The int main() is a given, so that can't be modified.

    When I compile it, errors are given, such as "Cannot convert int to Computer.."

    Can anyone point out my mistakes?

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Red face

    First, the function call is named show() not display as you defined in the class:

    Code:
               
          void display(int b,double c,int d)
                  
          {
             cpuSpeed=b;
             hardDrive=c;
             memory=d;
             cout<<"Custom "<<cpuSpeed<<" "<<hardDrive<<" "<<memory<<" "<<endl;
          }

    Also, why is display have a function arguement as char when you have not defined one.

    Look at your book- These are easy assignments your instructor is giving you.

    Mr. C
    Last edited by Mister C; 09-29-2002 at 06:25 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, first, you need some constructors for your Computer class if you want to pass arguments to them. Without the proper constructors, the following line will cause errors:

    Computer compaq(50,0.5,8), toshiba('A'), gateway('C');

    Second, there is no show method defined in Computer, you define display, but it doesn't take zero arguments.

    Finally, return ; should be return 0;.
    Code:
    #include<iostream>
    using namespace std;
    
    class Computer
    {
    public:
      Computer ( char a );
      Computer ( int b,double c,int d ) :
        cpuSpeed ( b ), hardDrive ( c ), memory ( d )
      {}
      void show();
    private:
      int cpuSpeed, memory;
      double hardDrive;
    };
    
    Computer::Computer ( char a )
    {
      switch ( a ) {
      case 'A':
        cpuSpeed = 75;
        hardDrive = 1;
        memory = 16;
        break;
      case 'B':
        cpuSpeed = 100;
        hardDrive = 1.5;
        memory = 16;
        break;
      case 'C':
        cpuSpeed = 133;
        hardDrive = 2;
        memory = 32;
      }
    }
    
    void Computer::show()
    {
      cout<<"CPU: "<< cpuSpeed
        <<"\nHD: "<< hardDrive
        <<"\nMem: "<< memory
        <<'\n'<<endl;
    }
    
    int main()
    {
      Computer compaq ( 50, 0.5, 8 ), toshiba ( 'A' ), gateway ( 'C' );
    
      compaq.show();
      toshiba.show();
      gateway.show();
    
      return 0;
    }
    >These are easy assignments your instructor is giving you.
    Maybe for you, but these kind of problems tend to give obscure error messages.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    Thanks guys.. I'm still really bad at Classes, since I learned them about 5 days ago.

    And my book is useless... the author has a hard time explaining C++ in "simple" terms. Thats why we never use them.

    And you're right, the program is really simple. But the error messages gave me no clue on what was wrong.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And my book is useless...
    I would recommend Accelerated C++ by Andrew Koenig and Barbara Moo. It is by far the best introductory text on C++, and it actually uses the language as it should be from the start.

    -Prelude
    My best code is written with the delete key.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    A book that I can agree with!!

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:34 AM
  2. Help with Classes Exercise
    By mscbuck in forum C++ Programming
    Replies: 13
    Last Post: 12-20-2007, 10:50 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. Help with Classes Exercise
    By Excalibur XP in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2002, 08:22 PM

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