Click here to Skip to main content
15,880,405 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Compiling MPI with VS2017 Linux Pin
leon de boer18-Mar-18 14:22
leon de boer18-Mar-18 14:22 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland18-Mar-18 23:21
mvaKenneth Haugland18-Mar-18 23:21 
GeneralRe: Compiling MPI with VS2017 Linux Pin
leon de boer19-Mar-18 5:02
leon de boer19-Mar-18 5:02 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 6:17
mvaKenneth Haugland19-Mar-18 6:17 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 7:07
professional Randor 19-Mar-18 7:07 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 9:06
mvaKenneth Haugland19-Mar-18 9:06 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 9:14
professional Randor 19-Mar-18 9:14 
Questionproblem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Tarun Jha17-Mar-18 1:30
Tarun Jha17-Mar-18 1: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 5:52
Victor Nijegorodov17-Mar-18 5:52 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
CPallini17-Mar-18 11:58
mveCPallini17-Mar-18 11:58 
QuestionConnection String Couldn't Connect Pin
Brennan Salibrici15-Mar-18 10:17
Brennan Salibrici15-Mar-18 10:17 
AnswerRe: Connection String Couldn't Connect Pin
Gerry Schmitz15-Mar-18 11:01
mveGerry Schmitz15-Mar-18 11:01 
AnswerRe: Connection String Couldn't Connect Pin
Victor Nijegorodov15-Mar-18 11:25
Victor Nijegorodov15-Mar-18 11:25 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 17:12
leon de boer15-Mar-18 17:12 
GeneralRe: Connection String Couldn't Connect Pin
Jochen Arndt15-Mar-18 22:10
professionalJochen Arndt15-Mar-18 22:10 
GeneralRe: Connection String Couldn't Connect Pin
Victor Nijegorodov16-Mar-18 8:41
Victor Nijegorodov16-Mar-18 8:41 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer18-Mar-18 4:20
leon de boer18-Mar-18 4:20 
AnswerRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 16:49
leon de boer15-Mar-18 16:49 
GeneralRe: Connection String Couldn't Connect Pin
Brennan Salibrici16-Mar-18 2:45
Brennan Salibrici16-Mar-18 2:45 
QuestionRe: Connection String Couldn't Connect Pin
David Crow16-Mar-18 16:43
David Crow16-Mar-18 16:43 
Questionproblem in operator overloading of >> & << ? Pin
Tarun Jha15-Mar-18 7:47
Tarun Jha15-Mar-18 7:47 
AnswerRe: problem in operator overloading of >> & << ? Pin
CPallini15-Mar-18 10:47
mveCPallini15-Mar-18 10:47 
QuestionCan we include two Message Maps in one class? Pin
Sampath57914-Mar-18 23:23
Sampath57914-Mar-18 23:23 
AnswerRe: Can we include two Message Maps in one class? Pin
Richard MacCutchan14-Mar-18 23:32
mveRichard MacCutchan14-Mar-18 23:32 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57914-Mar-18 23:39
Sampath57914-Mar-18 23: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.