I have few question, If i have two private member array in my class how can I use [] operator for my use, something like following,
class foo
{
int iarray[10];
char carray[10]
public:
//constructors
//copy cons nd assignment ans all
};
So my first question is When we have both in and char array and we cant overload [] operator, so whats the way to do use [] operator when we have more than two arrays.
My second problem is
#include<iostream>
#include<string.h>
using namespace std;
class base
{
private:
char* name;
char snaime[10];
public:
base();
~base();
base(const char*);
base(const base&);
void display(void);
base operator= (const base&);
friend base add(base,base);
friend ostream& operator<<(ostream&, base&);
friend istream& operator>>(istream&, base&);
char& operator[](const int);
};
base::base()
{
name = new char(0);
}
base::~base()
{
delete name;
}
base::base(const char* foo)
{
name = new char[strlen(foo)+1];
strcpy(name,foo);
}
base::base(const base& foo)
{
name = new char[strlen(foo.name)+1];
strcpy(name, foo.name);
}
base base::operator=(const base& rhs)
{
cout<<"Inside assignmnet .."<<endl;
if(this== &rhs)
{
cout<<"Same assignment creepo.."<<endl;
return *this;
}
delete name;
name = new char[strlen(rhs.name)+1];
strcpy(name,rhs.name);
return *this;
}
ostream& operator<<(ostream& outt, base& b1)
{
outt<<"Name chip: "<<b1.name<<endl;
return outt;
}
istream& operator>>(istream& inn, base& b1)
{
cout<<"Enter the name"<<endl;
inn>>b1.name;
return inn ;
}
char& base::operator[](const int index)
{
return sname[index];
}
int main()
{
base b1;
b1[0]="Gladiator";
cout<<b1[0]<<endl;
return 0;
}
Its giving me some "invalid conversion from ‘const char*’ to ‘char’
" ERROR .. So how to get rid of this.. Where I am doing wrong.
thanx in advance