0% found this document useful (0 votes)
45 views2 pages

CT1 Ans

The document provides the definition and test functions for a SafeString class, including a constructor to safely copy a string, a destructor to release memory, and functions to return the length of a string, check if two strings are equal, and safely concatenate one string to another. It also includes a C++ program that tests the SafeString class by creating SafeString objects, checking equality, and concatenating strings.

Uploaded by

Boss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

CT1 Ans

The document provides the definition and test functions for a SafeString class, including a constructor to safely copy a string, a destructor to release memory, and functions to return the length of a string, check if two strings are equal, and safely concatenate one string to another. It also includes a C++ program that tests the SafeString class by creating SafeString objects, checking equality, and concatenating strings.

Uploaded by

Boss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Department of Computer Science and Engineering

Bangladesh University of Engineering and Technology


CSE210: Object-Oriented Programming Language
Class Test-1, Marks-20, Time-20 Minutes

Q. Write a C++ program to define and test the functions of the following class. Do not use any
“cstring” or “string” library function.
class SafeString {
char *str;
public:
SafeString(char *s) ; //Constructor safely copies string s into string str
/* Destructor releases the allocated memory of str when a SafeString object goes out of scope */
~SafeString() ;
int length(char *s); //Returns the length of the string s
bool equals(char *s); //Boolean function returns true if strings str and s are same
void concatenates(char *s); //Safely concatenates s at the end of str
};

Answer:

#include <iostream>
using namespace std;

class SafeString {
char *str;
public:
SafeString(char *s) ; //Constructor safely copies string s into string str
/* Destructor releases the allocated memory of str when a SafeString object goes out of scope */
~SafeString() ;
int length(char *s); //Returns the length of the string s
bool equals(char *s); //Boolean function returns true if strings str and s are same
void concatenates(char *s); //Safely concatenates s at the end of str
char *getstr();
};

SafeString::SafeString(char *s){
int i, l;
l=length(s);
str=new char[l+1];
for(i=0;s[i];i++){
str[i]=s[i];
}
str[i]='\0';
}

SafeString::~SafeString(){
delete []str;
}

char *SafeString::getstr(){
return str;
}

int SafeString::length(char *s){


int l=0;
for(l=0;s[l];l++);
return l;
}

bool SafeString::equals(char *s){


int l1, l2,i;
l1=length(str);
l2=length(s);
if(l1!=l2){
return false;
}
else{
for(i=0;i<l1;i++){
if(str[i]!=s[i]){
return false;
}
}
return true;
}
}
void SafeString::concatenates(char *s){
int l1, l2, i,j;
l1=length(str);
l2=length(s);
char *temp=new char[l1+l2+1];
for(i=0;str[i];i++){
temp[i]=str[i];
}
for(j=0;s[j];i++, j++){
temp[i]=s[j];
}
temp[i]='\0';
delete []str;
str=temp;
}

int main(){
char str1[80]="I love C++";
char str2[80]="I love Bangladesh";
char str3[80]="I love C++";
SafeString s(str1);
cout<<s.getstr()<<endl;
if(s.equals(str3)){
cout<<"s.str is equal to str3"<<endl;
}
if(s.equals(str2)){
cout<<"s.str is equal to str2"<<endl;
}
s.concatenates(str2);
cout<<s.getstr()<<endl;

return 0;
}

You might also like