Solved

C++ problem for answer 2000

Posted on 1998-10-18
7
179 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
[X]
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
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 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