473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help for concatenation of string and object.

Hi,
this is the code to print s.

Expand|Select|Wrap|Line Numbers
  1. main() {
  2. sample s1(10,20,30);
  3. char *s;
  4. s="Hello"+s1;
  5. }
  6.  
  7.  friend char * operator +(char *s,sample &b) {
  8.         char *s1;
  9.         s1 = new char[50];
  10.         char *s2;
  11.         s2 = new char[10];
  12.         strcpy(s1,s);
  13.         strcat(s1,"Width");
  14.         sprintf(s2,"%g",b.width);
  15.         strcat(s1,s2);
  16.         strcat(s1,"Height");
  17.         sprintf(s2,"%g",b.height);
  18.         strcat(s1,s2);
  19.         strcat(s1,"depth");
  20.         sprintf(s2,"%g",b.depth);
  21.         strcat(s1,s2);
  22.         delete [] s2;
  23.  
  24.         return s1;
  25.         delete []s1;
  26.     }
  27.  
Could anybody help me for optimization?
May 31 '07 #1
8 1901
AdrianH
1,251 Expert 1GB
Hi,
this is the code to print s.

Expand|Select|Wrap|Line Numbers
  1. main() {
  2. sample s1(10,20,30);
  3. char *s;
  4. s="Hello"+s1;
  5. }
  6.  
  7.  friend char * operator +(char *s,sample &b) {
  8.         char *s1;
  9.         s1 = new char[50];
  10.         char *s2;
  11.         s2 = new char[10];
  12.         strcpy(s1,s);
  13.         strcat(s1,"Width");
  14.         sprintf(s2,"%g",b.width);
  15.         strcat(s1,s2);
  16.         strcat(s1,"Height");
  17.         sprintf(s2,"%g",b.height);
  18.         strcat(s1,s2);
  19.         strcat(s1,"depth");
  20.         sprintf(s2,"%g",b.depth);
  21.         strcat(s1,s2);
  22.         delete [] s2;
  23.  
  24.         return s1;
  25.         delete []s1;
  26.     }
  27.  
Could anybody help me for optimization?
Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


Adrian
May 31 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

Expand|Select|Wrap|Line Numbers
  1. stringstream str;
  2.  str << " Hello" << 10 << 20 << 30;
  3.  
  4. string result;
  5. str >> result;
  6.  
May 31 '07 #3
AdrianH
1,251 Expert 1GB
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?
I assumed it was part of the assignment.


Adrian
May 31 '07 #4
Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


Adrian
I used friend function because i need to make the statement char*s="Hello"+s1;
work. thats why i overloaded + using friend functions because the parameters iam passing are different.

I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..
Jun 1 '07 #5
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

Expand|Select|Wrap|Line Numbers
  1. stringstream str;
  2.  str << " Hello" << 10 << 20 << 30;
  3.  
  4. string result;
  5. str >> result;
  6.  

I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.
Jun 1 '07 #6
AdrianH
1,251 Expert 1GB
I used friend function because i need to make the statement char*s="Hello"+s1;
work. thats why i overloaded + using friend functions because the parameters iam passing are different.

I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..
Yes, but what I am saying is that you cannot declare a friend outside of a class and you’re not supposed to declare a friend with its body inside of one (well if I understand friend injection correctly, your case could work under the new standard, but you will start getting into trouble when you start doing this using templated classes so be careful, see this article for more info).


Adrian
Jun 1 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.
Why?

Just write an operator << for your classes and you are done.
Jun 1 '07 #8
AdrianH
1,251 Expert 1GB
Why?

Just write an operator << for your classes and you are done.
Yes, and by doing it with ostream as your left hand side (first) parameter, it would work for stringstream too since it inheirits from ostream.


Adrian
Jun 1 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
by: Trint Smith | last post by:
Ok, I have a webform that has these checkboxes: 1. something 2. something else 3. and something else When the user clicks on the checkbox, I want all of the selections to go into a textbox...
4
by: Lucas Tam | last post by:
Hi all, I'm concatenating a large SQL string for updating a table. There are > 80,000 commands (rows) in the SQL string. VB.NET seems to be *VERY* slow at string concatenation when the string...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
6
by: kellysgirl | last post by:
Option Explicit On Option Strict On Imports System.Globalization Public Class MainForm Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
4
by: eBob.com | last post by:
I am trying to understand a bit of JavaScript from http://developer.yahoo.com/maps/simple/jspost.html (appended below). I'd appreciate help understanding two things it. The first is the...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.