-
September 5th, 2013, 03:22 PM
#1
about class's and 'events'
in hedear file(.h):
Code:
class test{
private:
bool blVisible;
public:
void Show();
bool Visible()
{
return blVisible;
}
void Visible(bool value)
{
blVisible=value;
if (value==true) Show();//call the event
}
};
and in main.cpp:
Code:
#include <iostream>
#include "test.h"
#include <conio.h>
#include <fstream>
using namespace std;
test a;
void test::Show()
{
cout << "showed";
}
int main()
{
//read data
bool b;
cin >> b;
a.Visible(b);
cin.clear();
cin >> b;
a.Visible(b);
getch();
return 0;
}
just see these 2 lines:
Code:
test a;
void test::Show()
imagine that we do another: 'test c;'
the 'void test::Show()' will be the same and do the same cout
how can i work like these:
'void varname::Show()'??
-
September 5th, 2013, 08:19 PM
#2
Re: about class's and 'events'
Maybe it's just me, but the question doesn't seem clear. Are you trying to do this?
Test a;
a.Show();
Test b;
b.Show();
If not, can you clarify your question.
-
September 6th, 2013, 03:15 AM
#3
Re: about class's and 'events'
I agree with GCDEF that zour question is not clear.
Besides:
Originally Posted by Cambalinho
Code:
class test{
...
void Visible(bool value)
{
blVisible=value;
if (value==true) Show();//call the event
}
};
It is not a good style:
1. to place more than one statement in a line;
2. to compare boolean values with true;
Better style would be:
Code:
class test{
...
void Visible(bool value)
{
blVisible = value;
if (value)
Show();//call the event
}
};
Victor Nijegorodov
-
September 6th, 2013, 05:04 AM
#4
Re: about class's and 'events'
Originally Posted by GCDEF
Maybe it's just me, but the question doesn't seem clear. Are you trying to do this?
Test a;
a.Show();
Test b;
b.Show();
If not, can you clarify your question.
see again the 'correct' main.cpp:
Code:
#include <iostream>
#include "test.h"
#include <conio.h>
#include <fstream>
using namespace std;
test a;
void a::Show()
{
cout << "showed";
}
int main()
{
//read data
bool b;
cin >> b;
a.Visible(b);
cin.clear();
cin >> b;
a.Visible(b);
getch();
return 0;
}
see these:
Code:
test a;
test c;
void a::Show()
{
cout << "showed";
}
void c::Show()
{
cout << "show anotherone";
}
now that sub only works for 'a' and anotherone for 'c'. can i do these?
-
September 6th, 2013, 05:12 AM
#5
Re: about class's and 'events'
Originally Posted by Cambalinho
see these:
Code:
test a;
test c;
void a::Show()
{
cout << "showed";
}
void c::Show()
{
cout << "show anotherone";
}
What is a? Is it a class or an instance of class test?
If former - then what does this linemean?
If latter - then what does this "implementation"
Code:
void a::Show()
{
cout << "showed";
}
mean?
The same question - for c
Victor Nijegorodov
-
September 6th, 2013, 05:19 AM
#6
Re: about class's and 'events'
Originally Posted by VictorN
What is a? Is it a class or an instance of class test?
If former - then what does this line mean?
If latter - then what does this "implementation"
Code:
void a::Show()
{
cout << "showed";
}
mean?
The same question - for c
the 1st post
ok. i will repeat:
test.h:
Code:
#include <iostream>
class test
{
private:
bool blVisible;
public:
void Show();
bool Visible()
{
return blVisible;
}
void Visible(bool value)
{
blVisible=value;
if (value==true) Show();//call the event
}
};
now in main.cpp:
Code:
#include "test.h"
test a;
test c;
void a::Show()
{
cout << "showed";
}
void c::Show()
{
cout << "show anotherone";
}
int main()
{
//read data
bool b;
cin >> b;
a.Visible(b);
cin.clear();
cin >> b;
a.Visible(b);
getch();
return 0;
}
test is the class name
if i use:
these sub will be the same for all variables. that's why i'm asking if i can do these:
so the cout cound be diferent
can i do these?
-
September 6th, 2013, 05:25 AM
#7
Re: about class's and 'events'
Originally Posted by Cambalinho
the 1st post
ok. i will repeat:
Oh, please do NOT do it! Don't repost your fantasies!
Originally Posted by Cambalinho
test is the class name
if i use:
these sub will be the same for all variables.
Correct!
Originally Posted by Cambalinho
... that's why i'm asking if i can do these:
so the cout cound be diferent
can i do these?
No.
But you could derive a bew class(es) from test and overload or override this method in there.
Victor Nijegorodov
-
September 6th, 2013, 05:29 AM
#8
Re: about class's and 'events'
Originally Posted by VictorN
Oh, please do NOT do it! Don't repost your fantasies!
Correct!
No.
But you could derive a bew class(es) from test and overload or override this method in there.
how can i do it?
(you can give me a link for learn, please?)
-
September 6th, 2013, 05:34 AM
#9
Re: about class's and 'events'
If you do NOT know the basic of C++ then why do you ask it here in the Forum?
First read books, tutorials, blogs for the beginners. Test examples...
As for the link - Google is your friend.
And note that CG-Forum in not a grade school!
Victor Nijegorodov
-
September 6th, 2013, 05:47 AM
#10
Re: about class's and 'events'
Originally Posted by VictorN
If you do NOT know the basic of C++ then why do you ask it here in the Forum?
First read books, tutorials, blogs for the beginners. Test examples...
As for the link - Google is your friend.
And note that CG-Forum in not a grade school!
i know overload, what i don't is how i can do what you sugest
-
September 6th, 2013, 05:50 AM
#11
Re: about class's and 'events'
Victor Nijegorodov
-
September 6th, 2013, 05:53 AM
#12
Re: about class's and 'events'
Originally Posted by Cambalinho
i know overload, what i don't is how i can do what you sugest
No matter how many times you repeat it, your code makes no sense, therefore your question makes no sense. Look at what I posted earlier. If that's now what you're trying to do, explain using words, not code, exactly what you're trying to do.
-
September 6th, 2013, 05:57 AM
#13
Re: about class's and 'events'
Last edited by Cambalinho; September 6th, 2013 at 06:03 AM.
-
September 6th, 2013, 06:02 AM
#14
Re: about class's and 'events'
This is an example
Code:
#include <iostream>
using namespace std;
class c1 {
public:
virtual void show();
};
class c2 : public c1 {
public:
virtual void show();
};
void c1::show()
{
cout << "show in c1" << endl;
}
void c2::show()
{
cout << "show in c2" << endl;
}
void cshow(c1& c)
{
c.show();
}
int main()
{
c1 i1;
c2 i2;
cshow(i1);
cshow(i2);
return 0;
}
It produces the output
Code:
show in c1
show in c2
This is basic c++ class stuff. Study the book you're got!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 6th, 2013, 06:06 AM
#15
Re: about class's and 'events'
Originally Posted by 2kaud
This is an example
Code:
#include <iostream>
using namespace std;
class c1 {
public:
virtual void show();
};
class c2 : public c1 {
public:
virtual void show();
};
void c1::show()
{
cout << "show in c1" << endl;
}
void c2::show()
{
cout << "show in c2" << endl;
}
void cshow(c1& c)
{
c.show();
}
int main()
{
c1 i1;
c2 i2;
cshow(i1);
cshow(i2);
return 0;
}
It produces the output
Code:
show in c1
show in c2
This is basic c++ class stuff. Study the book you're got!
so i must use virtual functions. thanks for all to all
realy thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|