Write an example of C++ in C

  • 2020-05-26 09:53:14
  • OfStack

The following is the simulation implementation of string related functions, it includes the following functions:

String (const char * s); // USES a string to initialize an object
String (); // default constructor
String(const String & s); // copy constructor, which initializes the object using the String type
~ String (); // destructor
int length (); // returns the length of a string of type String
String & operator=(const String & s); // overload = operator.
String & operator=(const char *);
char & operator [] (int i); // overloads the [] operator
const char & operator[](int i) const;
friend bool operator < (const String & st,const String & st2); / / overloaded < Operator to compare the size of a string of type String.
friend bool operator > (const String & st,const String & st2);
friend bool operator==(const String & st,const String & st2); // overloading == operator to determine if two String objects are equal
friend ostream & operator < < (ostream & os,const String & st2); // overloaded output function
friend istream & operator > > (istream & is,String & st2); // overload the input function
static int HowMang()// returns the total number of String class objects generated.

String.h:


#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
#include"iostream"
#include<string.h>
using std::ostream;
using std::istream;
class String{
private:
  char * str;
  int len;
public:
  static int num_strings;
  static const int CINLM=80;
  String(const char * s);
  String();
  String(const String & s);
  ~String();
  int length();
  String & operator=(const String & s);
  String & operator=(const char *);
  char & operator[](int i);
  const char & operator[](int i) const;
  friend bool operator<(const String & st,const String & st2);
  friend bool operator>(const String & st,const String & st2);
  friend bool operator==(const String & st,const String & st2);
  friend ostream & operator<<(ostream & os,const String & st2);
  friend istream & operator>>(istream & is,String & st2);
  static int HowMang()
  {
    return num_strings;

  }
};


#endif // STRING_H_INCLUDED

String.cpp:


#include<iostream>
#include"String.h"
#include"string.h"
using namespace std;
int String::num_strings=0;
int String::length()
{
  return this->len;
}
  String::String(const char * s)
  {
    len=strlen(s);
    str=new char[len+1];
    num_strings++;
  }
  String::String()
  {
    str=0;
    len=0;
    num_strings++;
  }

  String::String(const String & s)
  {
    num_strings++;
    len=strlen(s.str);
    str=new char[len+1];
    strcpy(str,s.str);
  }
  String::~String()
  {
    --num_strings;
    delete [] str;
    len=0;
  }
  String & String::operator=(const String & s)
  {
    if(this==&s)
      return *this;
    delete [] str;
    len=strlen(s.str);
    str=new char[len+1];
    strcpy(str,s.str);
    // num_strings++;
  }
  String & String::operator=(const char * s)
  {
    len=strlen(s);
    str=new char[len+1];
    strcpy(str,s);
   // num_strings++;
  }
  char & String::operator[](int i)
  {
    return str[i];
  }
  const char & String::operator[](int i) const
  {
    return str[i];
  }
  bool operator<(const String & st,const String & st2)
  {
    if(strcmp(st.str,st2.str)<0)
      return true;
    else
      return false;
  }
  bool operator>(const String & st,const String & st2)
  {
    return (st<st2)==false;
  }
  bool operator==(const String & st,const String & st2)
  {
    if(strcmp(st.str,st2.str)>0)
      return true;
    else
      return false;
  }
  ostream & operator<<(ostream & os,const String & st2)
  {
    os<<st2.str;
    return os;
  }
  istream & operator>>(istream & is,String & st2)
  {
    char temp[String::CINLM];
    is.get(temp,String::CINLM);
    if(is)
      st2=temp;
    while(is&&is.get()!='\n')
      continue;
    return is;
  }

main.cpp:


#include <iostream>
#include"String.h"
using namespace std;
int main()
{
  String name[5];
  char temp[10];
  int i;
  for(i=0;i<5;i++)
  {
    cin.get(temp,10);
    while(cin&&cin.get()!='\n')
    continue;
    if(!cin&&temp[0]=='\0')// If it's an empty string, cin for false
     break;
    else
    name[i]=temp;
  }
  int total=i;
  int firs=0,shor=0;
  if(total<0)
  {
    cout<<" No input "<<endl;
  }else{
    for(i=0;i<total;i++)
    {
      cout<<name[i][0]<<":"<<name[i]<<endl;
    }
    for(i=0;i<total;i++)
    {
      if(name[i]<name[firs])
        firs=i;
      if(name[i].length()<name[shor].length())
        shor=name[i].length();
    }
  }
  cout<<" The shortest string is: "<<name[shor]<<endl;
  cout<<" The first string is: "<<name[firs]<<endl;
  return 0;
}

Related articles: