Inheritance project...

This is a discussion on Inheritance project... within the C++ Programming forums, part of the General Programming Boards category; Hey people, i have a few questions about my code here: a) on lines 7 and 8, doesn't aNum1 and ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    Inheritance project...

    Hey people, i have a few questions about my code here:

    a) on lines 7 and 8, doesn't aNum1 and aNum2 have to be declared because they are variables?

    b) on lines 11 and 12, would these two commands make the value of aNum1 and aNum2 0, and therefore make the output "0 0"?

    c) on lines 14, 17 and 20, why is void necessary at the begining of the line?

    d) i have to split this into two files (pair.h and pair.cpp), consisting of a header file which contains information on how the class is linked together (method code omitted), and the .cpp file which has the methods in it, how do i do this?

    Thanks!
    ----------------------------------------------------------------------------------
    [Code]:
    ----------------------------------------------------------------------------------
    #include <iostream.h>
    #include <string.h>

    class Pair {
    public:
    Pair (int num1, int num2) {
    aNum1 = num1;
    aNum2 = num2;
    }
    Pair () {
    aNum1 = 0;
    aNum2 = 0;
    }
    void output () {
    cout << aNum1 << " " << aNum2 << endl;
    }
    void setNum1 (int num) {
    aNum1 = num;
    }
    void setNum2 (int num) {
    aNum2 = num;
    }
    private:
    int aNum1;
    int aNum2;
    };

    main () {

    Pair numPair (12, 34);
    numPair.output ();
    numPair.setNum2 (21);
    numPair.output ();

    }

    ----------------------------------------------------------------------------------
    [Output]:
    ----------------------------------------------------------------------------------
    12 34
    12 21

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i do this?
    Code:
    // pair.h
    #ifndef PAIR_H__
    #define PAIR_H__
    class Pair
    {
      int aNum1;
      int aNum2;
    public:
      Pair (int num1, int num2);
      Pair ();
      void output ();
      void setNum1 (int num);
      void setNum2 (int num);
    };
    #endif
    Code:
    // pair.c
    #include <iostream.h>
    #include "header.h"
    
    Pair :: Pair (int num1, int num2) {
      aNum1 = num1;
      aNum2 = num2;
    }
    Pair :: Pair () {
      aNum1 = 0;
      aNum2 = 0;
    }
    void Pair :: output () {
      cout << aNum1 << " " << aNum2 << endl;
    }
    void Pair :: setNum1 (int num) {
      aNum1 = num;
    }
    void Pair :: setNum2 (int num) {
      aNum2 = num;
    }
    Code:
    // main.c
    #include <iostream.h>
    #include "header.h"
    
    int main () {
      Pair numPair (12, 34);
      numPair.output ();
      numPair.setNum2 (21);
      numPair.output ();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    answers

    a) You have already declared aNum1 and aNum2 - lines 24, 25
    b) No, when you create the objects you are using the other constructor to initialise aNum1 and aNUm2
    c) void is the return type of the function, ie there is no return type!
    d) See Prelude's answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 10:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM

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