Click here to Skip to main content
13,868,681 members

C / C++ / MFC

 
AnswerRe: Compiling MPI with VS2017 Linux Pin
leon de boer18-Mar-18 15:22
memberleon de boer18-Mar-18 15:22 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 0:21
professionalKenneth Haugland19-Mar-18 0:21 
GeneralRe: Compiling MPI with VS2017 Linux Pin
leon de boer19-Mar-18 6:02
memberleon de boer19-Mar-18 6:02 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 7:17
professionalKenneth Haugland19-Mar-18 7:17 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 8:07
professional Randor 19-Mar-18 8:07 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 10:06
professionalKenneth Haugland19-Mar-18 10:06 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 10:14
professional Randor 19-Mar-18 10:14 
Questionproblem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Tarun Jha17-Mar-18 2:30
memberTarun Jha17-Mar-18 2:30 
#include <iostream>
#include <cstring>
using namespace std;

class st{

	char *p;
	int len;

	public:
		st(){len=0; p=0;}		//creates null string
		st(const char *s);		//creates string from arrays
		st(const st &s);		//copy constructor
		~st(){delete p;}		//destructor

		//+ operator
		friend st operator + (const st &, const st &);

		//<= operator
		friend int operator <= (const st &, const st &);
		friend void show(const st);
};

st :: st(const char *s){
	len = strlen(s);
	p = new char[len+1];
	strcpy(p, s);
	//cout<<p<<"\n\n";
}

st :: st(const st &s){
	len = s.len;
	p = new char[len+1];
	strcpy(p, s.p);
	//cout<<p;
}

//overloading + operator
st operator + (const st &s, const st &t){
	st temp;
	temp.len = s.len + t.len;
	//cout<<temp.len<<" ";
	temp.p = new char[temp.len + 1];

	//cout<<s.p<<" ";
	strcpy(temp.p, s.p);
	//cout<<t.p<<" ";
	strcat(temp.p, t.p);
	//cout<<temp.p<<"\n\n";
	return(temp);
}

//overloading <= operator
int operator <= (const st &s, const st &t){
	int m = strlen(s.p);
	int n = strlen(t.p);

	if(m <= n)	return(1);
	else return(0);
}

void show(const st s){
	cout<<s.p;
}


int main(){
	st s1 = "New ";
	st s2 = "York";
	st s3 = "Delhi";
	//st string1=s1, string2=s2, string3=s1+s3;
	st string1, string2, string3;
	string1 = s1;
	string2 = s2;
	string3 = s3+s1;


	cout<<"\nstring1  =  "; show(string1);
	cout<<"\nstring2  =  "; show(string2);
	cout<<endl;
	cout<<"\nstring3  =  "; show(string3);
	cout<<"\n\n";

	if(string1 <= string2){
		show(string1);	cout<<" smaller than ";	show(string2);
		cout<<endl;
	}else {
		show(string2);	cout<<" smaller than "; show(string2);
		cout<<endl;
	}

	return 0;
}


This program works fine when i replace
Quote:
st string1, string2, string3;

with
Quote:
st string1=s1, string2=s2, string3=s1+s3;


so what's the problem, i learned in class that we can pass the values in the constructor by calling it explicitly, and (string1, string2, string3) & (s1, s2, s3) have same return type i.e.class st.
So why isn't it working ?

thank you
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Victor Nijegorodov17-Mar-18 6:52
memberVictor Nijegorodov17-Mar-18 6:52 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
CPallini17-Mar-18 12:58
mveCPallini17-Mar-18 12:58 
QuestionConnection String Couldn't Connect Pin
Brennan Salibrici15-Mar-18 11:17
memberBrennan Salibrici15-Mar-18 11:17 
AnswerRe: Connection String Couldn't Connect Pin
Gerry Schmitz15-Mar-18 12:01
mveGerry Schmitz15-Mar-18 12:01 
AnswerRe: Connection String Couldn't Connect Pin
Victor Nijegorodov15-Mar-18 12:25
memberVictor Nijegorodov15-Mar-18 12:25 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 18:12
memberleon de boer15-Mar-18 18:12 
GeneralRe: Connection String Couldn't Connect Pin
Jochen Arndt15-Mar-18 23:10
mveJochen Arndt15-Mar-18 23:10 
GeneralRe: Connection String Couldn't Connect Pin
Victor Nijegorodov16-Mar-18 9:41
memberVictor Nijegorodov16-Mar-18 9:41 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer18-Mar-18 5:20
memberleon de boer18-Mar-18 5:20 
AnswerRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 17:49
memberleon de boer15-Mar-18 17:49 
GeneralRe: Connection String Couldn't Connect Pin
Brennan Salibrici16-Mar-18 3:45
memberBrennan Salibrici16-Mar-18 3:45 
QuestionRe: Connection String Couldn't Connect Pin
David Crow16-Mar-18 17:43
memberDavid Crow16-Mar-18 17:43 
Questionproblem in operator overloading of >> & << ? Pin
Tarun Jha15-Mar-18 8:47
memberTarun Jha15-Mar-18 8:47 
AnswerRe: problem in operator overloading of >> & << ? Pin
CPallini15-Mar-18 11:47
mveCPallini15-Mar-18 11:47 
QuestionCan we include two Message Maps in one class? Pin
Sampath57915-Mar-18 0:23
memberSampath57915-Mar-18 0:23 
AnswerRe: Can we include two Message Maps in one class? Pin
Richard MacCutchan15-Mar-18 0:32
protectorRichard MacCutchan15-Mar-18 0:32 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 0:39
memberSampath57915-Mar-18 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190214.1 | Last Updated 22 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid