Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #92250 > unrolled thread
| Started by | karam hello <karamhello2@gmail.com> |
|---|---|
| First post | 2023-09-29 12:59 -0700 |
| Last post | 2023-09-30 16:57 +0200 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.c++
a problem with an program karam hello <karamhello2@gmail.com> - 2023-09-29 12:59 -0700
Re: a problem with an program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-29 21:13 +0100
Re: a problem with an program "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-10-01 16:00 +0200
Re: a problem with an program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-10-01 16:29 +0100
Re: a problem with an program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-10-04 01:56 +0100
Re: a problem with an program David Brown <david.brown@hesbynett.no> - 2023-09-30 16:57 +0200
| From | karam hello <karamhello2@gmail.com> |
|---|---|
| Date | 2023-09-29 12:59 -0700 |
| Subject | a problem with an program |
| Message-ID | <fbfd5e3e-ccff-470d-b2b4-7090c1649e6an@googlegroups.com> |
i have a problem with this program using C++ OOP inheritance specially on class c:
#include <iostream>
using namespace std;
class a{
private:
string name;
int age;
public:
a(string n, int a){
name = n;
age = a;
}
};
class b{
private:
string email;
public:
b(string em){
email = em;
}
};
class c:public a, public b{
public:
c(string n, int a, string em) : a(n, a), b(em){}
};
int main(){
c c1("k", 10, "com");
}
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2023-09-29 21:13 +0100 |
| Message-ID | <87v8bsagai.fsf@bsb.me.uk> |
| In reply to | #92250 |
karam hello <karamhello2@gmail.com> writes:
> i have a problem with this program using C++ OOP inheritance
> specially on class c:
Your compiler should be telling your what's wrong. It might not do so
very clearly, but it should be pointing you to where the problem is.
But let me play compiler here and give you a different error message...
> #include <iostream>
> using namespace std;
> class a{
> private:
> string name;
> int age;
> public:
> a(string n, int a){
> name = n;
> age = a;
> }
> };
> class b{
> private:
> string email;
> public:
> b(string em){
> email = em;
> }
> };
> class c:public a, public b{
> public:
> c(string n, int a, string em) : a(n, a), b(em){}
line 23 position 41 ______________________^
a is an int. I can't call a(n, a) when a is an int.
> };
> int main(){
> c c1("k", 10, "com");
> }
Does that help?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach@gmail.com> |
|---|---|
| Date | 2023-10-01 16:00 +0200 |
| Message-ID | <ufbu2m$1j79g$1@dont-email.me> |
| In reply to | #92252 |
On 2023-09-29 10:13 PM, Ben Bacarisse wrote:
> karam hello <karamhello2@gmail.com> writes:
>
>> i have a problem with this program using C++ OOP inheritance
>> specially on class c:
>
> Your compiler should be telling your what's wrong. It might not do so
> very clearly, but it should be pointing you to where the problem is.
>
> But let me play compiler here and give you a different error message...
>
>> #include <iostream>
>> using namespace std;
>> class a{
>> private:
>> string name;
>> int age;
>> public:
>> a(string n, int a){
>> name = n;
>> age = a;
>> }
>> };
>> class b{
>> private:
>> string email;
>> public:
>> b(string em){
>> email = em;
>> }
>> };
>> class c:public a, public b{
>> public:
>> c(string n, int a, string em) : a(n, a), b(em){}
> line 23 position 41 ______________________^
> a is an int. I can't call a(n, a) when a is an int.
>
>> };
>> int main(){
>> c c1("k", 10, "com");
>> }
>
> Does that help?
g++ complains about that thing, but Visual C++ compiles the code just
fine as C++17.
Sort of, Visual C++ is consistent (base classes treated the same as data
members), while g++ is conforming. They're both "con". Just differently.
I guess learning point is, to be both consistent and conforming never
choose the same name for a constructor parameter and sub-object.
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2023-10-01 16:29 +0100 |
| Message-ID | <87a5t2cqeh.fsf@bsb.me.uk> |
| In reply to | #92288 |
"Alf P. Steinbach" <alf.p.steinbach@gmail.com> writes:
> On 2023-09-29 10:13 PM, Ben Bacarisse wrote:
>> karam hello <karamhello2@gmail.com> writes:
>>
>>> i have a problem with this program using C++ OOP inheritance
>>> specially on class c:
>> Your compiler should be telling your what's wrong. It might not do so
>> very clearly, but it should be pointing you to where the problem is.
>> But let me play compiler here and give you a different error message...
>>
>>> #include <iostream>
>>> using namespace std;
>>> class a{
>>> private:
>>> string name;
>>> int age;
>>> public:
>>> a(string n, int a){
>>> name = n;
>>> age = a;
>>> }
>>> };
>>> class b{
>>> private:
>>> string email;
>>> public:
>>> b(string em){
>>> email = em;
>>> }
>>> };
>>> class c:public a, public b{
>>> public:
>>> c(string n, int a, string em) : a(n, a), b(em){}
>> line 23 position 41 ______________________^
>> a is an int. I can't call a(n, a) when a is an int.
>>
>>> };
>>> int main(){
>>> c c1("k", 10, "com");
>>> }
>> Does that help?
>
> g++ complains about that thing, but Visual C++ compiles the code just fine
> as C++17.
Curious. Which one is correct? Without studying the standard, I would
have thought that gcc has it right.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2023-10-04 01:56 +0100 |
| Message-ID | <87edibb3y6.fsf@bsb.me.uk> |
| In reply to | #92288 |
"Alf P. Steinbach" <alf.p.steinbach@gmail.com> writes:
> On 2023-09-29 10:13 PM, Ben Bacarisse wrote:
>> karam hello <karamhello2@gmail.com> writes:
>>
>>> i have a problem with this program using C++ OOP inheritance
>>> specially on class c:
>> Your compiler should be telling your what's wrong. It might not do so
>> very clearly, but it should be pointing you to where the problem is.
>> But let me play compiler here and give you a different error message...
>>
>>> #include <iostream>
>>> using namespace std;
>>> class a{
>>> private:
>>> string name;
>>> int age;
>>> public:
>>> a(string n, int a){
>>> name = n;
>>> age = a;
>>> }
>>> };
>>> class b{
>>> private:
>>> string email;
>>> public:
>>> b(string em){
>>> email = em;
>>> }
>>> };
>>> class c:public a, public b{
>>> public:
>>> c(string n, int a, string em) : a(n, a), b(em){}
>> line 23 position 41 ______________________^
>> a is an int. I can't call a(n, a) when a is an int.
>>
>>> };
>>> int main(){
>>> c c1("k", 10, "com");
>>> }
>> Does that help?
>
> g++ complains about that thing, but Visual C++ compiles the code just fine
> as C++17.
Curious. Which one is correct I wonder.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2023-09-30 16:57 +0200 |
| Message-ID | <uf9d08$uldu$1@dont-email.me> |
| In reply to | #92250 |
On 29/09/2023 21:59, karam hello wrote:
> i have a problem with this program using C++ OOP inheritance specially on class c:
> #include <iostream>
> using namespace std;
> class a{
> private:
> string name;
> int age;
> public:
> a(string n, int a){
> name = n;
> age = a;
> }
> };
> class b{
> private:
> string email;
> public:
> b(string em){
> email = em;
> }
> };
> class c:public a, public b{
> public:
> c(string n, int a, string em) : a(n, a), b(em){}
> };
> int main(){
> c c1("k", 10, "com");
> }
It looks like you are learning the language. I recommend getting in the
habit of using good names for your classes (and everything else), rather
than "a" or "b". Good naming makes code easier to write, and vastly
easier to read, and you should practice it from the start, even for
simple test code. See if it helps you out here.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web