The 'name' (variable?) in line 6 is declared using some string functions from <string.h>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class person
{
public:
person(char*s)
{
name=newchar[strlen(s+1)];
strcpy(name,s);
}
void print()
{
cout<<"My name is "<<name<<endl;
}
protected:
char*name;
};
Gives me this error:
"[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]"
The concerned line is this: person x("ABC");
What is at fault here?
The program when typed in school (in Turbo C++) works fine.
class student: public person
{
float rno;
public:
student (char*s,float r): person(s),rno(r)
{}
void print()
{
cout<<"My name is "<<name<<" | My roll no. "<<rno;
}
};
int main()
{
person*p;
person x("ABC");
p=&x;
p->print();
student y("PQR",101);
p=&y;
p->print();
}
The output is only:
My name is ABC
My name is PQR
Why doesn't the cout operation associated with Roll no. work?
The problem is that "ABC" is a string literal, and points to constant, unmodifiable data in your program.
But making this be a "char*" implies that is modifiable.
Your person & student constructors should take in const char*, not a char*.
The program when typed in school (in Turbo C++) works fine.
You should complain, politely, about this. Turbo C++ is probably older than you are and doesn't support any of the C++ standards. At least that's my understanding.
You should complain, politely, about this. Turbo C++ is probably older than you are and doesn't support any of the C++ standards. At least that's my understanding.
That is the academic curriculum here. I can't do anything about it.
I mean, the State Board of Education refuses to change the Computer Science syllabus.
The Central Board (Countrywide) of Education Has Python for Computer Science.
hang in there. 1/2 my team is from india and they are quite good at it in spite of any extra challenges from the education system. Just be aware of what you are being taught and where it is insufficient so you can pick up some modern stuff on the side.
One idea may be to get a free version g++, which can be had free on any unix / linux etc or via cygwin for windows, if you have a pc that you can install software on. Then run it with strict language flags so you at least know what isnt being taught well.
The core ideas of looping, logic, and such have not changed. Focus on the principles and try not to get tangled up in the language specifics. The last version of turbo was in the mid 90s, before it moved to borland's gui / builder.
if you get in trouble with c-strings again, just ask. We may hate on them, but most of us older coders know them unfortunately very well.