Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ problem for answer 2000

Posted on 1998-10-18
7
177 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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