Hi,

I get some problems when I run the following codes.

#include<iostream.h>

class Distance
{
protected:
    int feet;
    float inches;
public:
    Distance() { feet = 0; inches = 0; }
    Distance(int ft, float in)
    {
        feet = ft;
        inches = in;
    }

    void get_dist()
    {
        cout << "\n Enter feet "; cin >> feet;
        cout << "\n Enter inches "; cin >> inches;
    }

    void show_dist()
    {
        cout << feet << "\'-" << inches << "\"";
    }
};

enum posneg { pos, neg };

class Dist_Sign:public Distance
{
private: 
    posneg sign;

public:
    Dist_Sign():Distance() { sign = pos; }
    Dist_Sign(int ft, float in, poseng sg = pos):Distance(ft, in) { sign = sg; }

    void get_dist()
    {
        Distance::get_dist();
        char ch;
        cout << "Enter sign(+ or -): "; cin >> ch;
        sign = (ch=='+')?pos:neg;
    }

    void show_dist()
    {
        cout << ((sign==pos)? "(+)":"(-)")    ;
        Distance::show_dist();
    }
};

void main()
{
    Dist_Sign alpha;
    alpha.get_dist();
    Dist_Sign beta(11, 6.25);
    Dist_Sign gamma(100, 5.5, neg);

    cout << "\n alpha = "; alpha.show_dist();
    cout << "\n beta = "; beta.show_dist();
    cout << "\n gamma = "; gamma.show_dist();
}

That shows (4) errors.

1. unexpected 'class Dist_Sign ('
2. unexpected token(s) preceding ':'; skipping apparent function body
3. 'Dist_Sign::Dist_Sign' : no overloaded function takes 2 parameters
4. 'Dist_Sign::Dist_Sign' : no overloaded function takes 3 parameters

Regards,
zawpai

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Why would I care?
After 69 posts you're still using void main, and still can't use code tags.

Just look at what you've posted man. Unindented crap full of smilies. I've got better things to do that figure out what that supposed to mean.

Hi Salem,


[TEX]After 69 posts you're still using void main, and still can't use code tags.[/TEX]

Actually, after niek_e and mitrmkar told me to use the code tags, I try to use it. You will see that I am not using it. I just use the wrong code tags, isn't it?

Related to posting ...

In case you haven't noticed, there is also a "Preview Post" button available. Press that button and you'll see a preview of how your post will look like. It does not submit your post, it just displays the preview.

Yeah if you can't understand how to use simple bbcode, where is point od teaching you C++? You should try just a bit making people think on what you posted. My brain threw "Confused by earlier errors, bearing out".

Hi all,

Sorry for my careless mistakes ;)

But........... I still cann't solve this problem.:'(
Who can give me a hand?

Many Thanks....:)

1 error I can see right away: Dist_Sign(int ft, float in, poseng sg = pos "poseng " should be "posneg ". It's a typo. Now repost your code using code-tags and you'll probably get a lot more help then that.

And some more free advice:
- dump your (Turbo C??) compiler, it's outdated and doesn't support standard c++
- it's #include <iostream> without the '.h'. Once you've updated compiler you'll see why
- void main() doesn't exist. (no matter what your teacher told you). It's 'int main()' and be sure to 'return 0;'

Hi niek_e,

After changing the error that you told me, the program can run well.

#include<iostream.h>

class Distance
{
protected:
	int feet;
	float inches;
public:
	Distance() { feet = 0; inches = 0; }
	Distance(int ft, float in)
	{
		feet = ft;
		inches = in;
	}

	void get_dist()
	{
		cout << "\n Enter feet "; cin >> feet;
		cout << "\n Enter inches "; cin >> inches;
	}

	void show_dist()
	{
		cout << feet << "\'-" << inches << "\"";
	}
};

enum posneg { pos, neg };

class Dist_Sign:public Distance
{
private: 
	posneg sign;

public:
	Dist_Sign():Distance() { sign = pos; }
	Dist_Sign(int ft, float in, posneg sg = pos):Distance(ft, in) { sign = sg; }

	void get_dist()
	{
		Distance::get_dist();
		char ch;
		cout << "Enter sign(+ or -): "; cin >> ch;
		sign = (ch=='+')?pos:neg;
	}

	void show_dist()
	{
		cout << ((sign==pos)? "(+)":"(-)")	;
		Distance::show_dist();
	}
};

void main()
{
	Dist_Sign alpha;
	alpha.get_dist();
	Dist_Sign beta(11, 6.25);
	Dist_Sign gamma(100, 5.5, neg);

	cout << "\n alpha = "; alpha.show_dist();
	cout << "\n beta = "; beta.show_dist();
	cout << "\n gamma = "; gamma.show_dist();
}

dump your (Turbo C??) compiler, it's outdated and doesn't support standard c++ Actually, I am using VC++ compiler. Will void main() effect my program? Because I am learning old C++ book that I got from my friend.

Ok, I will use int main() and return 0 next time.

Thank you for giving me a hand.

Best Regards,
zawpai

Hi all,

Could I ask one more question?
What is the difference by using 'private' and 'public' in derived class.
Eg: class DistSign:public Distance and class DistSign:private Distance Thanks

Hi all,
Could I ask one more question?

Well.... ok, but just this once ;)

What is the difference by using 'private' and 'public' .

Private means that the variable is only accessible inside the class. Public means it can be accessed by anyone.
Here's an example to make things clearer:

class DistSign
{
private: 
    int priv;
public:
    int publ;
    void ShowMe(void) 
    {
         std::cout << "private: " << priv << " public: " << publ << "\n";
     }
};

int main()
{
    DistSign D;
    std::cout << "private: " << D.priv << " public: " << D.publ << "\n";
    cin.get();
    return 0;
}

When compiling this code you'll get an error on line 17, but not on line 10. That's because the line 10 is inside the class and 17 is not.

Well.... ok, but just this once ;)

Private means that the variable is only accessible inside the class. Public means it can be accessed by anyone.
Here's an example to make things clearer:

class DistSign
{
private: 
    int priv;
public:
    int publ;
    void ShowMe(void) 
    {
         std::cout << "private: " << priv << " public: " << publ << "\n";
     }
};

int main()
{
    DistSign D;
    std::cout << "private: " << D.priv << " public: " << D.publ << "\n";
    cin.get();
    return 0;
}

When compiling this code you'll get an error on line 17, but not on line 10. That's because the line 10 is inside the class and 17 is not.

that's not what was asked

that's not what was asked

So I've noticed. I misread the post, but then mitrmkar posted a good link, so I didn't repost.

This article has been dead for over six months. Start a new discussion instead.