I want to make three[0]='p'; work in the code below. I think I have to make an operator overloading for that but I don't know how to do. What I want to get is to change first index of "Lottery winner!" to 'p'. (To get "pottery winner!").
#include<iostream>
#include<cstring>
using namespace std;
class str
{
char* a;
public:
str(char *aa=""){
this->a = new char[strlen(aa)+1];
strcpy(a,aa);
}
~str(){
delete a;
}
friend ostream& operator<<(ostream &out, str &aa);
friend istream& operator>>(istream &in, str &aa);
};
ostream& operator<<(ostream &out, str &aa){
out<<aa.a;
return out;
}
istream& operator>>(istream &in, str &aa){
in>>aa.a;
return in;
}
void main(){
str three("Lottery winner!");
three[0]='p';
cout<<three<<endl;
}
This is the general signature of the operator[]:
T& operator[](any_type);
In your context it would look like this:
struct str {
...
char& operator[](std::size_t pos) {
return a[pos];
}
};
class str
{
// ...
public:
// ...
char& operator[] (int x)
{
// add array out-of-bounds check here if you like to ...
return a[x];
}
}
operator char*()
{
return a;
}
Could also work
Related
I have the following code I was testing:
#include<bits/stdc++.h>
using namespace std;
class Stringtype{
char* str;
int length;
public:
Stringtype(){
str='\0';
length=0;
}
Stringtype(const Stringtype& s){
cout<<"Constructor"<<endl;
strcpy(this->str, s.str);
this->length = s.length;
}
Stringtype(char* text){
strcpy(this->str, text);
this->length = strlen(str);
}
friend ostream& operator<<(ostream& stream, Stringtype s){
stream<<"Value :"<<endl;
for(int i=0;i<s.length;i++){
cout<<(s.str)[i];
}
cout<<endl;
stream<<"Length :"<<endl;
stream<<s.length<<endl;
return stream;
}
friend istream& operator>>(istream& stream, Stringtype& s){
cout<<"Enter the string"<<endl<<endl;
s.str = new char[30];
cin>>s.str;
s.length = strlen(s.str);
return stream;
}
};
int main(){
Stringtype s1, s2;
cin>>s1>>s2;
cout<<s1<<s2;
cout<<(s1>s2)<<endl;
cout<<(s1<s2)<<endl;
cout<<(s1==s2)<<endl;
cout<<(s1+s2)<<endl;
return 0;
}
This code produces the following output (after the input is taken properly):
Constructor
and then crashes.
I can't seem to understand why...
Any help is greatly appreciated. Thank you.
You never actually reserve memory for your internal memory representation of your string. You need to have a new somewhere. Otherwise you will write to memory you don't own, which is exactly what is happening here, right in the next line after the output you observe.
By the way: there should be no copy-construction here, your signature
friend ostream& operator<<(ostream& stream, Stringtype s){
should read:
friend ostream& operator<<(ostream& stream, const Stringtype& s){
Doesn't change the fact that you have errors in your memory handling though.
Please change the following
friend ostream& operator<<(ostream& stream, Stringtype s)
to
friend ostream& operator<<(ostream& stream, Stringtype& s)
Take the reference for the string s
Try initializing str="", instead str='\0';
I am kind of newbie to programming migrated from legacy turbo c++ to VS C++2012,I have a tough time catching up and i wanted to emulate the string library for TC. But i cant make the insertion operator work in this code....Please help Out. Could you tell the mistake i made in this code. And also why are we returning the object via reference for overloading.
#include<iostream>
#include<string>
namespace String
{
class string
{
char word[100];
int size;
public:
string()
{
size=0;
}
string(int sz)
{
size=sz;
}
string(char *Word)
{
strcpy(word,Word);
size=sizeof(*Word);
}
~string()
{
}
string &operator+(string Add)
{
strcat(word,Add.word);
return *this;
}
string &operator=(char *Word)
{
strcpy(word,Word);
return *this;
}
/*
ostream &operator<<(ostream &sout,string Show)
{
sout<<Show.word;
return sout;
}
*/
void Show()
{
std::cout<<word;
}
};
}
void main()
{
String::string A="ABCDEF";
String::string B="GHIJK";
String::string C;
C=A+B;
C.Show();
std::cin.ignore(2);
//std::cout<<C;
}
You should declare operator<< as a non-member function, because ostream will be taken as the 1st argument for operator<<, a user define type's member function can't satisfy it.
namespace String
{
class string
{
...
public:
ostream& put(ostream &sout) { sout << word; return sout; }
};
ostream& operator<<(ostream& sout, string Show) { return Show.put(sout); }
}
The output operator << has to be overloaded in the namespace, not the class itself if you want to be able to use it like so:
cout << my_class_object;
So, in the declaration of your class (string.h) add this line:
ostream &operator<<(ostream & sout,const string & Show);
And then in the definition file (string.cpp) in your namespace, not the class itself, ad this function:
ostream & operator<<( ostream & out, const bigint & data )
{
// the printing implementation
}
Hi I have this ostream operator that gives me this error when I compile: cannot access private member declared in class 'CService'
here is my code:
friend ostream& operator <<(ostream& os, CService &obj);
ostream& operator<<(ostream &os, CService &obj) {
os<<obj.m_strClient;
return os;
}
I tried returning ostream& but that doesn't fix the problem, instead it adds another error syntax error : ';'
Here is my entire code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class CService {
private:
string m_strClient;
string m_strSeller;
int m_iMinutes;
public:
CService() {
m_strClient="N/A";
m_strSeller="N/A";
m_iMinutes=0;
}
CService( string c, string s, int m ) {
m_strClient=c;
m_strSeller=s;
m_iMinutes=m;
}
CService(const CService &obj ) {
m_strClient=obj.m_strClient;
m_strSeller=obj.m_strSeller;
m_iMinutes=obj.m_iMinutes;
}
string GetClient() {
return m_strClient;
}
string GetSeller() {
return m_strSeller;
}
int GetMusic() {
return m_iMinutes;
}
CService CService::operator =(CService obj) {
m_strClient=obj.m_strClient;
m_strSeller=obj.m_strSeller;
m_iMinutes=obj.m_iMinutes;
return *this;
}
bool operator < (const CService &obj) const {
return m_strSeller<obj.m_strSeller;
}
CService CService::operator +(const CService &obj) const {
return CService( m_iMinutes+obj.m_iMinutes );
}
friend ostream& operator <<(ostream& os, CService &obj);
bool CService::operator==(CService &obj) {
return (obj.m_strSeller==m_strSeller);
}
};
ostream& operator<<(ostream &os, CService &obj) {
os<<obj.m_strClient;
return ostream&;
}
I am a beginner trying to learn c++ so probably my question is very basic. Consider the following pice of code:
class pounds
{
private:
int m_p;
int m_cents;
public:
pounds(){m_p = 0; m_cents= 0;}
pounds(int p, int cents)
{
m_p = p;
m_cents = cents;
}
friend ostream& operator << (ostream&, pounds&);
friend istream& operator>>(istream&, pounds&);
};
ostream& operator<< (ostream& op, pounds& p)
{
op<<p.m_p<<"and "<<p.m_cents<<endl;
return op;
}
istream& operator>>(istream& ip, pounds& p)
{
ip>>p.m_p>>p.m_cents;
return ip;
}
This compiles and seems to work but I am not returning a reference to a local variable? Thanks in advance.
It's correct, since there are no local variables, there are references, that will be passed, when operators will be called.
And i suggest you to change signature of operator << to
std::ostream& operator << (ostream& os, const pounds& p);
since, p is not modified in function.
Given a class such as:
class Person
{
private:
char *name;
public:
Person()
{
name = new char[20];
}
~Person()
{
delete [] name;
}
}
I want to print to print the name from an instance of this, using a statement like the following:
cout << myPerson << endl;
What do I need to do to define the << output operator for this class?
add this in the class:
friend std::ostream& operator<< (std::ostream& out, const Person& P);
and then define the operator<< something like this:
std::ostream& operator<< (std::ostream& out, const Person& P) {
out << P.name;
return out;
}
Define a member function print() that takes an ostream as an argument. Then let the overloaded operator<< call this member function. This way you can avoid using friend. Example:
void YourClass::print(ostream& out) const
{
//implement printing ...
}
ostream& operator<<(ostream& out, const YourClass& m)
{
m.print(out);
return out;
}