#include<iostream.h>
class Exforsys();

{
    private:
        int a;
    public:
        Exforsys()
        {}
        Exforsys(int w)
    {
        a=w;
    }
    Exforsys(Exforsys& e)
    {
        a=e.a;
        cout<<"Example of Copy Constructor";
    }
    void result()
    {
        cout<<a;
    }

    void main()
    {
    Exforsys e1(50);
    Exforsys e3(e1); cout<<"\ne3="; e3.result();
    } 
}

Recommended Answers

All 5 Replies

3 problems

Declaration of a copy constructor should be of a const reference variable parameter and not just a reference variable parameter--

Exforsys(const Exforsys& e)

--you might get away with it during compile/run but your professor may dock you points.

second, not sure if void main() is going to work on all c++ compilers, use int main and return 0.

finally, I'm pretty sure you have to type "using namespace std;" to use cout... not to mention the missing endl or flush statement after pushing a string into the stream.

You need to have a global main too.

::main();

And please use code tags around your code, it helps those who wants to help you :)

I dont think all above changes are needed.

This will work

#include<iostream.h>
using namespace std;           
class Exforsys
{
private:
   int a;
public:
   Exforsys()
   {}
   Exforsys(int w)
   {
   a=w;
   }
   Exforsys(Exforsys& e)
   {
   a=e.a;
   cout<<"Example of Copy Constructor";
   }
    void result()
   {
   cout<<a;
   }
};

int main()
{
Exforsys e1(50);
Exforsys e3(e1);
cout<<"\ne3="<<e3.result();
return 0;
}

It's not just a matter of code that works, but good practices also.

i tried the above code posted by stevanity but there is error which is:

error C2143: syntax error : missing ';' before '<<'

but when i place ";" before '<<', there is still an error...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.