Solved

C++ problem for answer 2000

Posted on 1998-10-18
7
173 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now