Solved

C++ problem for answer 2000

Posted on 1998-10-18
7
178 Views
Last Modified: 2010-04-02
would like to have the output for this program but I'm unable to get it to run. Can you help, please?



#include <string.h>
#include <iostream.h>

class Test{
public:
Test(string name):n(name){ cout << "Made Test"<<n<<endl;}
virtual ~Test() {cout << "Finished Test"<<n<<endl;}
virtual void f(){cout << "Using Test"<<n<<endl;}
private:
      string n;
};
class Prog:public Test
{public:
Prog(string name, int val):Test(name), v(val)
{cout <<"Made Prog"<<name<<"with"<<v<<endl;}
~Prog(){cout<<"Finished Prog"<<v<<endl;}
void f(){cout<<"Using Prog"<<v<<endl;}
private:
      int v;
};
void main(){
      Test *t=new Prog("code",123);
      t.f();
}
0
Comment
Question by:nelsonab
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1175500
First, change the first two lines to:
    #include <string>
    #include <iostream>

Next, add the line
    using namespace std;
Or precede every reference to string and cout with "std::".

And lastly, change the last line to
    t->f();  //*****

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1175501
Couple of other things too:n should be defined before you use it



   class Test{
   private:
   string n;
   public:
   Test(string name):n(name){ cout << "Made Test"<<n<<endl;}
   virtual ~Test() {cout << "Finished Test"<<n<<endl;}
   virtual void f(){cout << "Using Test"<<n<<endl;}
   };

Recommend adding
delete t ;

as last line in main
0
 
LVL 5

Expert Comment

by:yonat
ID: 1175502
Oops, I must be doing too many things at once - I didn't notice the "for Answers2000" at the title when I answered this question - sorry.

You are right about the "delete t;" of course. Another option is to replace the last two lines with:
    Prog t("code",123);
    t.f();

As for defining n before using it, this is not necessary: According to the standard, the following code
    class C {
        void foo() {cout << s;}
        string s;
    };
is equivalent to
    class C {
        void foo();
        string s;
    };

    inline void C::foo() {cout << s;}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1175503
I haven't checked the standard on this, but I should point out that several compilers in common use, do require definition before use.

As we don't know the compiler version in use, this may be an issue.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1175504
The question is: Where is n first used? Writing the implementation of a member function as part of the class declaration is just a syntactical convenience. The actual *use* occurs *after* the class declaration. I don't have the standard here, but in Stroustrup 2nd Ed. this is stated at r.9.3.2 .

As for compilers that actually require "seeing" the the member data before "seeing" the inline member function that uses it - I haven't used such a compiler yet, so I didn't know this was an issue. Also, I didn't find anything about this at http://www.mozilla.org/docs/tplist/catBuild/portable-cpp.html , which lists many C++ portability issues.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1175505
Actually I forgot n is a template - so you are right in this case.

For non-template's variable, it's a whole diff question.

0
 
LVL 5

Expert Comment

by:yonat
ID: 1175506
Can you give a reference for this? As far as I know, the rule applies even if n is not a template. (Please forgive my being pedant, by I thought I knew the language pretty well, and your comments surprise me.)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 181
How do i run a c++ file? 15 51
Where are the c++ header files to use in Eclipse? 4 46
I could not set window to top 4 40
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question