A string in C++ is simply an array of characters with the null character(\\0) us
ID: 3781510 • Letter: A
Question
A string in C++ is simply an array of characters with the null character()
used to mark the end of the string. C++ provides a set of string handling function in
<string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard
Template Library), C++ now provides a string class.
But for this assignment, you are to develop your own string class. This will give you a
chance to develop and work with a C++ class, define constructors and destructors, define
member functions outside of the class body, develop a copy constructor and assignment
operator (and understand why!), work with C-Style strings and pointers, dynamically
allocate memory and free it when done.
Of course, you must also do suitable testing which implies writing a main function that
uses your new string class.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
class String
{
public:
String()
{
cout << "Constructor called";
}
~String()
{
cout << "Destructor called";
}
};
int main()
{
String obj1; // Constructor Called
int x=1;
if(x)
{
String obj2; // Constructor Called
} // Destructor Called for obj2
}
and outside member function can be describe
#include<bits/stdc++.h>
using namespace std;
class rectangle
{
private:
int m,n;
public:
void setdata(int,int);
void area();
};
void rectangle::setdata(int u,int v)
{
m=u;
n=v;
}
void rectangle::area()
{
int a=m*n;
cout<<" Area of Rectangle= "<<a;
}
int main()
{
rectangle f,s;
f.setdata(10,20);
cout<<"Rectangle First : ";
f.area();
s.setdata(20,30);
cout<<" Rectangle Second :";
s.area();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.