Author Topic: problem with inheritence in c++.  (Read 3105 times)

0 Members and 1 Guest are viewing this topic.

Offline annefrank

  • /dev/null
  • *
  • Posts: 11
  • Cookies: -1
    • View Profile
problem with inheritence in c++.
« on: April 07, 2013, 08:30:15 pm »
//A program related to "Quiz". I have  3 lifelines to use in any number of ques. but my program is giving some kind of inheritence error. here, I have done programming upto 2 ques only my 1st ques is running but when i come up with second question with many  possibilities it shows  some kind of error. please help me. 
 
Code: ( cpp) [Select]

#include<iostream.h>
#include<conio.h>
class A
{
public:
int fif,aud,ask,r,P;
void life1();   //  lifeline declaration. when none of the lifeline is used.
};
void A::life1()
{
fif=0;
ask=0;
aud=0;
P=0;
cout<<"You have 3 lifelines:\n";
cout<<"Audience Poll\n";
cout<<"Computer Ask\n";
cout<<"Fifty fifty\n";
cin>>r;
if(r==1)
{
aud++;  // get added when audience poll will be used.
}
if(r==2)
{
ask++;
}
if(r==3)
{
fif++;
}
P++;      //get added by 1 when any one of them is used
}
class Ba : public A  // when one from three of them is used.
{
public:
int t,O;
void life2a();
};
void Ba::life2a()
{
O=0;
cout<<"You have 2 lifelines:\n";
cout<<"Audience Poll\n";
cout<<"Fifty Fifty\n";
cin>>t;
if(t==1)
{
aud++;
}
if(t==2)
{
fif++;  // get added when fifty-fifty will be used
}
O++;  // get added by 1 when any one of them will be used.
}
class Bb : public A // when one from three of them is used
{
public:
int y,L;
void life2b();
};
void Bb:: life2b()
{
L=0;
cout<<"You have 2 lifelines:\n";
cout<<"Audience Poll\n";
cout<<"Computer Ask\n";
cin>>y;
if(y==1)
{
aud++;
}
if(y==2)
{
ask++;
}
L++;
}
class Bc: public A // when one from three of them is used
{
public:
int u,Y;
void life2c();
};
void Bc::life2c()
{
Y=0;
cout<<"You have 2 lifelines:\n";
cout<<"Computer Ask\n";
cout<<"Fifty-Fifty\n";
cin>>u;
if(u==1)
{
ask++;
}
if(u==2)
{
fif++;
}
Y++;
}
class Ca: public A // when only one lifeline left to use.
{
public:
int K;
void life3a();
};
void Ca::life3a()
{
K=0;
cout<<"You have only 1 lifeline:\n";
cout<<"Computer ask\n";
ask++;
K++;
}
class Cb: public A // when only one lifeline left to use.
{
public:
int J;
void life3b();
};
void Cb::life3b()
{
J=0;
cout<<"You have only 1 lifeline:\n";
cout<<"Fifty-Fifty\n";
fif++;
J++;
}
class Cc : public A // when only one lifeline left to use.
{
public:
int B;
void life3c();
};
void Cc::life3c()
{
B=0;
cout<<"You have only 1 lifeline:\n";
cout<<"Audience Poll\n";
aud++;
B++;
}
class ques
{
public:
int ans1,ans2,ans3;
char q,z;
void ques1()
{
cout<<"Which  planet is called as REd planet ?\n";
cout<<"your options are :\n";
cout<<"1.Mars\n";
cout<<"2.jupiter\n";
cout<<"3.earth\n";
cout<<"4.uranus";
cout<<"\nif you want to use lifeline then enter y else n";
cin>>q;
if(q=='y'||q=='Y')
{
void life1();
{
if(r==1)
{
cout<<"1.80%\n";
cout<<"2.10%\n";
cout<<"3.6%\n";
cout<<"4.4%\n";
}
else if(r==2)
{
cout<<"Mars\n";
}
else (r==3)
{
cout<<"Mars /earth\n";
}
}
else
{
cout<<"Give an answer:\n";
cin>>ans1;
switch(ans1)
{
case 1:
cout<<"right answer\n";
break;
case 2:
cout<<"sorry your answer is wrong\n";
break;
case 3:
cout<<"sorry your answer is wrong\n";
break;
case 4:
cout<<"sorry your answer is wrong\n";
break;
}
if(ans1==1)
{
cout<<"You won Rs.1000\n"
}
}
}
void ques2()
{
cout<<"Your second ques is:\n";
cout<<"Light year is a unit of?";
cout<<"Your options are:\n intensity of light ";
cout<<"2. distance\n";
cout<<"3. time\n";
cout<<"4.planetary motion\n";
cout<<"\nif you want lifeline then press y else n:\n ";
cin>>z;
if(z=='y'||z=='Y')
{
if(P==1&&ask==1)
{
life2a();
if(t==1)
{
cout<<"\n1. 10%\t";
cout<<"2. 80%\t";
cout<<"3. 6%\t";
cout<<"4. 4%\n";
}
if(t==2)
{
cout<<"\ndistance\n/time\n";
}
}
else if(P==1&&aud==1)
{
life2c();
if(u==1)
{
cout<<"\ndistance\n/time\n";
}
if(u==2)
{
cout<<"distance\n";
}
}
else if(P==1&&fif==1)
{
life2b();
if(y==1)
{
cout<<"\n1. 10%\t";
cout<<"2. 80%\t";
cout<<"3. 6%\t";
cout<<"4. 4%\n";
}
if(y==2)
{
cout<<"distance\n";
}
}
else
{
life1();
if(r==1)
{
cout<<"1.10%\n";
cout<<"2.80%\n";
cout<<"3.6%\n" ;
cout<<"4.4%\n";
}
if(r==2)
{
cout<<"\ndistance\n/time \n";
}
if(r==3)
{
cout<<"\distance\n";
}
}
}
else
{
cout<<"Give ans:\n";
cin>>ans2;
switch(ans2)
{
case 1:
cout<<"sorry your answer is wrong\n";
break;
case 2:
cout<<" your answer is right\n";
break;
case 3:
cout<<"sorry your answer is wrong\n";
break;
case 4:
cout<<"sorry your answer is wrong\n";
break;
}
if(ans2==2)
{
cout<<"You won Rs.2000\n";
}
}
};
 
void main()
{
clrscr();
ques q;
q.ques1();
q.ques2();
getch();
}
« Last Edit: April 08, 2013, 07:44:41 pm by annefrank »

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: problem with inheritence in c++.
« Reply #1 on: April 07, 2013, 08:32:24 pm »
Oh god WHY!!!!!!, Please use [code ][/code ] tags and proper formatting...
« Last Edit: April 07, 2013, 08:33:58 pm by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline annefrank

  • /dev/null
  • *
  • Posts: 11
  • Cookies: -1
    • View Profile
Re: problem with inheritence in c++.
« Reply #2 on: April 07, 2013, 08:41:27 pm »
Oh god WHY!!!!!!, Please use [code ][/code ] tags and proper formatting...
thanks.. :)

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with inheritence in c++.
« Reply #3 on: April 07, 2013, 08:47:54 pm »
I tried compiling the program . Some of the errors were related to escape sequencing . Why would you do this ?
cout<<"\earth";
there is no \e escape sequence , a bunch of errors were like this .
Secondly

else(){} doesn't make sense to me
else if(){} is the correct way .

Third use#include <iosteam> not iostream.h
Fourth , use std::cout,std::endl,std::cin or "using namespace std;" before main .
Now I haven't run the program but it compiled properly , Nobody can help you with the inheritance problem or if the program behaves abnormally . You didn't mention what this program was supposed to do and what was the problem(after execution).

And p.s don't make a function inline when it is more than 3 lines of code , I see you've made a few functions inline which are like 30 lines long.
« Last Edit: April 07, 2013, 08:53:15 pm by pllaybuoy »
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: problem with inheritence in c++.
« Reply #4 on: April 07, 2013, 08:53:38 pm »
Please use this instead anne: [c ode=cpp] [/code ] and replace it with this.
Code: (cpp) [Select]
#include<iostream.h>
#include<conio.h>
class A
{
public:
    int fif,aud,ask,r,P;
    void life1();   //  lifeline declaration. when none of the lifeline is used.
};
void A::life1()
{
    fif=0;
    ask=0;
    aud=0;
    P=0;
    cout<<"You have 3 lifelines:\n";
    cout<<"Audience Poll\n";
    cout<<"Computer Ask\n";
    cout<<"Fifty fifty\n";
    cin>>r;
    if(r==1)
    {
        aud++;  // get added when audience poll will be used.
    }
    if(r==2)
    {
        ask++;
    }
    if(r==3)
    {
        fif++;
    }
    P++;      //get added by 1 when any one of them is used
}
class Ba : public A  // when one from three of them is used.
{
public:
    int t,O;
    void life2a();
};
void Ba::life2a()
{
    O=0;
    cout<<"You have 2 lifelines:\n";
    cout<<"Audience Poll\n";
    cout<<"Fifty Fifty\n";
    cin>>t;
    if(t==1)
    {
        aud++;
    }
    if(t==2)
    {
        fif++;  // get added when fifty-fifty will be used
    }
    O++;  // get added by 1 when any one of them will be used.
}
class Bb : public A // when one from three of them is used
{
public:
    int y,L;
    void life2b();
};
void Bb:: life2b()
{
    L=0;
    cout<<"You have 2 lifelines:\n";
    cout<<"Audience Poll\n";
    cout<<"Computer Ask\n";
    cin>>y;
    if(y==1)
    {
        aud++;
    }
    if(y==2)
    {
        ask++;
    }
    L++;
}
class Bc: public A // when one from three of them is used
{
public:
    int u,Y;
    void life2c();
};
void Bc::life2c()
{
    Y=0;
    cout<<"You have 2 lifelines:\n";
    cout<<"Computer Ask\n";
    cout<<"Fifty-Fifty\n";
    cin>>u;
    if(u==1)
    {
        ask++;
    }
    if(u==2)
    {
        fif++;
    }
    Y++;
}
class Ca: public A // when only one lifeline left to use.
{
public:
    int K;
    void life3a();
};
void Ca::life3a()
{
    K=0;
    cout<<"You have only 1 lifeline:\n";
    cout<<"Computer ask\n";
    ask++;
    K++;
}
class Cb: public A // when only one lifeline left to use.
{
public:
    int J;
    void life3b();
};
void Cb::life3b()
{
    J=0;
    cout<<"You have only 1 lifeline:\n";
    cout<<"Fifty-Fifty\n";
    fif++;
    J++;
}
class Cc : public A // when only one lifeline left to use.
{
public:
    int B;
    void life3c();
};
void Cc::life3c()
{
    B=0;
    cout<<"You have only 1 lifeline:\n";
    cout<<"Audience Poll\n";
    aud++;
    B++;
}
class ques
{
public:
    int ans1,ans2,ans3;
    char q,z;
    void ques1()
    {
        cout<<"Which is planet is called as REd planet ?\n";
        cout<<"your options are :\n";
        cout<<"1.Mars\n";
        cout<<"2.jupiter\n";
        cout<<"3.earth\n";
        cout<<"4.uranus";
        cout<<"\nif you want to use lifeline then enter y else n";
        cin>>q;
        if(q=='y'||q=='Y')
        {
            void life1();
            {
                if(r==1)
                {
                    cout<<"1.80%\n";
                    cout<<"2.10%\n";
                    cout<<"3.6%\n";
                    cout<<"4.4%\n";
                }
                else if(r==2)
                {
                    cout<<"Mars\n";
                }
                else (r==3)
                {
                    cout<<"Mars \earth\n";
                }
            }
            else
            {
                cout<<"Give an answer:\n";
                cin>>ans1;
                switch(ans1)
                {
                case 1:
                    cout<<"right answer\n";
                    break;
                case 2:
                    cout<<"sorry your answer is wrong\n";
                    break;
                case 3:
                    cout<<"sorry your answer is wrong\n";
                    break;
                case 4:
                    cout<<"sorry your answer is wrong\n";
                    break;
                }
                if(ans1==1)
                {
                    cout<<"You won Rs.1000\n"
                }
            }
        }
            void ques2()
        {
            cout<<"Your second ques is:\n";
            cout<<"Light year is a unit of?";
            cout<<"Your options are:\n intensity of light ";
            cout<<"2. distance\n";
            cout<<"3. time\n";
            cout<<"4.planetary motion\n";
            cout<<"\nif you want lifeline then press y else n:\n ";
            cin>>z;
            if(z=='y'||z=='Y')
            {
                if(P==1&&ask==1)
                {
                    life2a();
                    if(t==1)
                    {
                        cout<<"\n1. 10%\t";
                        cout<<"2. 80%\t";
                        cout<<"3. 6%\t";
                        cout<<"4. 4%\n";
                    }
                    if(t==2)
                    {
                        cout<<"\ndistance\n/time\n";
                    }
                }
                else if(P==1&&aud==1)
                {
                    life2c();
                    if(u==1)
                    {
                        cout<<"\ndistance\n/time\n";
                    }
                    if(u==2)
                    {
                        cout<<"distance\n";
                    }
                }
                else if(P==1&&fif==1)
                {
                    life2b();
                    if(y==1)
                    {
                        cout<<"\n1. 10%\t";
                        cout<<"2. 80%\t";
                        cout<<"3. 6%\t";
                        cout<<"4. 4%\n";
                    }
                    if(y==2)
                    {
                        cout<<"distance\n";
                    }
                }
                else
                {
                    life1();
                    if(r==1)
                    {
                        cout<<"1.10%\n";
                        cout<<"2.80%\n";
                        cout<<"3.6%\n" ;
                        cout<<"4.4%\n";
                    }
                    if(r==2)
                    {
                        cout<<"\ndistance\n/time \n";
                    }
                    if(r==3)
                    {
                        cout<<"\distance\n";
                    }
                }
            }
            else
            {
                cout<<"Give ans:\n";
                cin>>ans2;
                switch(ans2)
                {
                case 1:
                    cout<<"sorry your answer is wrong\n";
                    break;
                case 2:
                    cout<<" your answer is right\n";
                    break;
                case 3:
                    cout<<"sorry your answer is wrong\n";
                    break;
                case 4:
                    cout<<"sorry your answer is wrong\n";
                    break;
                }
                if(ans2==2)
                {
                    cout<<"You won Rs.2000\n";
                }
            }
        };


        void main()
        {
            clrscr();
            ques q;
            q.ques1();
            q.ques2();
            getch();
        }
I tried compiling the program . Some of the errors were related to escape sequencing . Why would you do this ?
cout<<"\earth";
there is no \e escape sequence , a bunch of errors were like this .
Secondly

else(){} doesn't make sense to me
else if(){} is the correct way .

Third use#include <iosteam> not iostream.h
Fourth , use std::cout,std::endl,std::cin or "using namespace std;" before main .
Now I haven't run the program but it compiled properly , Nobody can help you with the inheritance problem or if the program behaves abnormally . You didn't mention what this program was supposed to do and what was the problem(after execution).
else(){} makes perfect sense, it runs if if() returns false.


Oh and just saw that you are using void main(); try to avoid using void there, most of us don't like it.
As for the error, I don't get the one you are talking about, what compiler are you using?
And yes, I fixed the "<*.h>" and "using * *;"
« Last Edit: April 07, 2013, 08:57:13 pm by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline annefrank

  • /dev/null
  • *
  • Posts: 11
  • Cookies: -1
    • View Profile
Re: problem with inheritence in c++.
« Reply #5 on: April 07, 2013, 09:25:05 pm »
I tried compiling the program . Some of the errors were related to escape sequencing . Why would you do this ?
cout<<"\earth";
there is no \e escape sequence , a bunch of errors were like this .
Secondly

else(){} doesn't make sense to me
else if(){} is the correct way .

Third use#include <iosteam> not iostream.h
Fourth , use std::cout,std::endl,std::cin or "using namespace std;" before main .
Now I haven't run the program but it compiled properly , Nobody can help you with the inheritance problem or if the program behaves abnormally . You didn't mention what this program was supposed to do and what was the problem(after execution).

And p.s don't make a function inline when it is more than 3 lines of code , I see you've made a few functions inline which are like 30 lines long.
I dont know about "using namespace std". I have never used . Can you please elaborate?
and i also dont know about inline functions, i got the inline function error many times but never understand what really it is?

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with inheritence in c++.
« Reply #6 on: April 07, 2013, 09:27:22 pm »
i dont know about "using namespace std". I have never used . Can you please elaborate?
and i also dont know about inline functions, i got the inline function error many times but never understand what really it is?
A namespace is where variables are declared for later use , std is a namespace in which cout and cin are declared as objects of ostream and istream classes (respectively).
Without writing "using namespace std" before main , cout,cin,endl and any other thing declared in std is not available to the program so it gives 'cout/cin undeclared' error.
An alternative to writing "using namespace std" (and the preferred way) is by using the scope resolution operator ::
So you should use std::cout instead of cout and std::cin instead of cin (same for every name that is declared in std ).

As for inline functions it is a little difficult to explain but I'll try .
What happens when you call a function(mind I am using call here , not define) in the code ?
example

void myfunction(void){std::cout<<"hey";} //function definition
int main(){
myfunction(); //function called
}

When you execute the program and the execution reaches the line where myfunction is called , it looks for the function's definition  Now this process takes a little time , as execution jumps from the line where myfunction was called to the line where it is defined .
Making an function 'inline' means that the execution will not have to jump from here to there looking for function but instead the compiler  copies and pastes the function definition everywhere the function is called(before turning it into the .exe) .
so
inline void myfunction(void){std::cout<<"hey";}
int main(){
myfunction();//called here
}

this time the execution won't jump from the line where it is called to the line where it is defined instead it will do this
int main(){
void myfunction(void){std::cout<<"hey";}
myfuntion();//called here
}

So you see ? Its like it pastes the function definition everywhere the function is called , the program might run a bit faster since it will not have to jump from here to there BUT you are trading speed with space. It is obvious that it will take more space since it has more lines of code . Therefore,only functions which are 3-4 lines long are to be made inline .Imagine  a function of hundred linepasted everywhere in the code , it will make the code lengthy and thus take more space.

There are two ways to make a function inline ,
*either by the use of the keyword inline before its prototype(or definition if you don't give the prototype separately)
*or by giving the function definition inside(instead of defining the function outside of class declaration e.g: void class_name::myfunction(); ) the class declaration (This doesn't require mentioning the keyword inline before prototype) .
« Last Edit: April 07, 2013, 09:36:02 pm by pllaybuoy »
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline annefrank

  • /dev/null
  • *
  • Posts: 11
  • Cookies: -1
    • View Profile
Re: problem with inheritence in c++.
« Reply #7 on: April 07, 2013, 09:29:36 pm »

 
       
     
 

As for the error, I don't get the one you are talking about, what compiler are you using?
And yes, I fixed the "<*.h>" and "using * *;"
i am using turbo c++

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: problem with inheritence in c++.
« Reply #8 on: April 07, 2013, 09:56:54 pm »
i am using turbo c++
Please throw that antique thing away and get a newer compiler, like GCC.

Reasons to not use Turbo C++
« Last Edit: April 07, 2013, 10:00:17 pm by Fur »

Offline pune3t

  • Serf
  • *
  • Posts: 20
  • Cookies: -6
  • Not an Engineer but a Socialist in my way
    • View Profile
Re: problem with inheritence in c++.
« Reply #9 on: April 08, 2013, 04:10:10 am »
Please throw that antique thing away and get a newer compiler, like GCC.

Reasons to not use Turbo C++
lo.. and anne I thought You know how to use inline functions and other things too, but I guess I was wrong... And coming back to your program, +1 for wirelessdesert...
If you think you can do a thing or think you can't do a thing, you're right.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: problem with inheritence in c++.
« Reply #10 on: April 08, 2013, 07:56:47 am »
Also I don't see any correct class usage. I think you are expecting the class members to pop up in global namespace. That is wrong. Correct class usage is - MyClass myInstance; create a instance of the class. Then you would access the member function like this myInstance.memberFunction.
~Factionwars

Offline annefrank

  • /dev/null
  • *
  • Posts: 11
  • Cookies: -1
    • View Profile
Re: problem with inheritence in c++.
« Reply #11 on: April 08, 2013, 07:20:06 pm »
1. Also I don't see any correct class usage. I think you are expecting the class members to pop up in global namespace.
2. That is wrong. Correct class usage is - MyClass myInstance; create a instance of the class. Then you would access the member function like this myInstance.memberFunction.
I didn't get you actually. I have used object to call the member functions of a class at the last in void main same as above told by you. Am i wrong? If yes then tell me the right  method.