Sunday, March 20, 2011

Cpp program 6

/*Q6. Write a program for the String class in which you can define a constant string, which holds the value "Hello". The program should ask the user to input a name.
The name should be concatented with the constan string and displayed as "Hell My_Name".
Use new and delete operators to allocate and deallocate memory for strings.

Hint : Assign the constant to another operator and deallocate memory for strings.*/

# include "iostream"
using namespace std;
# include "stdio.h"
# include "string.h"

class String
{
public:
char *cStr ;
public:



void display(String SS)
{
String Temp;
char ch[100];

this->cStr = new char[100];
this->cStr = "Hello ";

strcpy(ch,this->cStr);
strcat(ch,SS.cStr);


Temp.cStr = ch;

puts(Temp.cStr);



}



};





int main()
{




printf("Enter the Name\n");
String Object;
Object.cStr = new char[100];
gets(Object.cStr);
Object.display(Object);






return 0;
}

No comments:

Post a Comment