Solved

C++ problem for answer 2000

Posted on 1998-10-18
7
174 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
ADO Memory leak with DELPHI 2007 37 171
how to convert c++ code to Android App 3 83
is twain_32.dll cmpatible with windows 10 ? 10 125
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 …
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 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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now