1、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/03
From:C++ primer plus 第十二章编程练习 第1题
*********************************************************************************************//***********************************cow.h***************************************************/
#ifndef COW_H_
#define COW_H_
#includeusing namespace std;
class Cow
{char name[20];char *hobby;double weight;
public:Cow();Cow(const char * nm, const char * ho, double wt);Cow(const Cow &c);~Cow();Cow & operator=(const Cow & c);void ShowCow() const;
};
#endif
/***********************************cow.cpp***************************************************/
#include"stdafx.h"
#include"cow.h"Cow::Cow()
{strcpy_s(name, "fuckfuck");hobby = new char[10];strcpy_s(hobby,10, "eat");weight = 0.0;
}
Cow::Cow(const char * nm, const char * ho, double wt)
{strcpy_s(name, nm);hobby = new char[strlen(ho) + 1];strcpy_s(hobby, strlen(ho) + 1, ho);weight = wt;
}
Cow::Cow(const Cow &c)
{strcpy_s(name, c.name);hobby = new char[strlen(c.hobby) + 1];strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);weight = c.weight;
}Cow::~Cow()
{delete[]hobby;
}Cow & Cow::operator=(const Cow & c)
{if (this == &c)return *this;else{strcpy_s(name, c.name);delete[]hobby;hobby = new char[strlen(c.hobby) + 1];strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);weight = c.weight;return *this;}
}void Cow::ShowCow() const
{cout <<"name::" <}
/***********************************main.cpp***************************************************/
#include"stdafx.h"
#include"cow.h"
int main()
{Cow c1;c1.ShowCow();Cow c2("moumou", "drink", 43.6);c2.ShowCow();Cow c3(c2);c3.ShowCow();c1 &#61; c2;c1.ShowCow();return 0;
}
2、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/04
From:C&#43;&#43; primer plus 第十二章编程练习 第2题
*********************************************************************************************/
/***********************************string2.h**********************************************************/#ifndef STRING2_H_
#define STRING2_H_#include
#include
using namespace std;
class String
{
private:char * str;int len;static const int CINLIM &#61; 80;public:String();String(const char * s);String(const String & st);String & operator&#61;(const String &st);String & operator&#61;(const char *s);~String();void stringlow();void stringup();int has(char a);String operator&#43;(const char * s);friend String operator&#43;(const char * s,String st);friend String operator&#43;(String st1, String st2);friend ostream & operator<<(ostream & os, const String & st);friend istream & operator>>(istream & is, String & st);friend bool operator&#61;&#61;(const String &st1, const String &st2);};#endif
/***********************************string2.cpp**********************************************************/
#include"stdafx.h"
#include"string2.h"String::String()
{len &#61; 4;str &#61; new char[1];str[0] &#61; &#39;\0&#39;;}
String::String(const char * s)
{len &#61; strlen(s);str &#61; new char[len &#43; 1];strcpy_s(str, len &#43; 1, s);
}
String::String(const String & st)
{len &#61; st.len;str &#61; new char[len &#43; 1];strcpy_s(str, len &#43; 1, st.str);
}
String & String::operator&#61;(const String &st)
{if (&st &#61;&#61; this)return *this;else{delete[]str;len &#61; st.len;str &#61; new char[len &#43; 1];strcpy_s(str, len &#43; 1, st.str);return *this;}
}
String & String::operator&#61;(const char *s)
{delete[]str;len &#61; strlen(s);str &#61; new char[len &#43; 1];strcpy_s(str, len &#43; 1, s);return *this;
}
String::~String()
{delete[]str;
}
void String::stringlow()
{for (int i &#61; 0; i }
void String::stringup()
{for (int i &#61; 0; i }
int String::has(char a)
{int s&#61;0;for (int i &#61; 0; i }
String String::operator&#43;(const char * s)
{int l &#61; len &#43; strlen(s);char * p &#61; new char[l &#43; 1];strcpy_s(p, l&#43;1, str);strcat_s(p, l&#43;1, s);return String(p);
}
String operator&#43;(const char * s, String st)
{int l &#61; strlen(s) &#43; st.len;char *p &#61; new char[l &#43; 1];strcpy_s(p, l&#43;1, s);strcat_s(p, l&#43;1, st.str);return String(p);
}
String operator&#43;(String st1, String st2)
{int l &#61; st1.len &#43; st2.len;char * p &#61; new char[l &#43; 1];strcpy_s(p, l&#43;1, st1.str);strcat_s(p, l&#43;1,st2.str);return String(p);
}
ostream & operator<<(ostream & os, const String & st)
{os <}
istream & operator>>(istream & is, String & st)
{char temp[String::CINLIM];is.get(temp, String::CINLIM);if (is)st &#61; temp;while (is&&is.get() !&#61; &#39;\n&#39;)continue;return is;
}
bool operator&#61;&#61;(const String &st1, const String &st2)
{return(strcmp(st1.str, st2.str)&#61;&#61;0);
}
/***********************************main.cpp**********************************************************/
#include"stdafx.h"
#include"string2.h"
int main()
{String s1(" and I am a C&#43;&#43; student.");String s2 &#61; "Please enter your name: ";String s3;cout <> s3;s2 &#61; "My name is " &#43; s3;cout <> ans){ans.stringlow();for (int i &#61; 0; i <3; i&#43;&#43;){if (ans &#61;&#61; rgb[i]){cout <<"That&#39;s right!\n";success &#61; true;break;}}if (success)break;elsecout <<"Try again!\n";}cout <<"Bye\n";return 0;}
3、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/04
From:C&#43;&#43; primer plus 第十二章编程练习 第3题
*********************************************************************************************/
/***********************************stock20.h**********************************************************/
#ifndef STOCK20_H_
#define STOCK20_H_#include
using namespace std;
class stock
{
private:char * company;int shares;double share_val;double total_val;void set_tot(){ total_val &#61; shares*share_val; }
public:stock();stock(const char * s, long n &#61; 0, double pr &#61; 0.0);~stock();void buy(long num, double price);void sell(long num, double price);void update(double price);const stock & topval(const stock & s)const;friend ostream & operator<<(ostream & os, const stock & sto);};
#endif
/***********************************stock20.cpp**********************************************************/
#include"stdafx.h"
#include"stock20.h"
stock::stock()
{company &#61; new char[1];company[0] &#61; &#39;\0&#39;;shares &#61; 0;share_val &#61; 0.0;total_val &#61; 0.0;
}
stock::stock(const char * s, long n, double pr)
{company &#61; new char[strlen(s) &#43; 1];strcpy_s(company, strlen(s) &#43; 1, s);if (n <0){cout <<"Number of shares can&#39;t be negative;" <}
stock::~stock()
{delete[]company;
}
void stock::buy(long num, double price)
{if (num <0){cout <<"Number of shares purchased can&#39;t be negative. " <<"Transaction is aborted.\n";}else{shares &#43;&#61; num;share_val &#61; price;set_tot();}
}void stock::sell(long num, double price)
{if (num <0){cout <<"Number of shares sold can&#39;t be negative. " <<"Transaction is aborted.\n";}else if (num>shares){cout <<"You can&#39;t sell more than you have! " <<"Transaction is aborted.\n";}else{shares -&#61; num;share_val &#61; price;set_tot();}
}void stock::update(double price)
{share_val &#61; price;set_tot();
}
const stock & stock::topval(const stock & s)const
{if (s.total_val > total_val)return s;elsereturn *this;
}ostream & operator<<(ostream & os, const stock & sto)
{ios_base::fmtflags orig &#61; os.setf(ios_base::fixed, ios_base::floatfield);streamsize prec &#61; os.precision(3);os <<"company: " <}
/***********************************main.cpp**********************************************************/
#include"stdafx.h"
#include"stock20.h"const int STKS &#61; 4;
int main()
{stock stocks[STKS] &#61;{stock("NanoSmart", 12, 20.0),stock("Boffo Objects", 200, 2.0),stock("Monolithic Obelisks", 130, 3.25),stock("Fleep Enterprises", 60, 6.5)};cout <<"stock holdings:\n";int st;for (st &#61; 0; st topval(stocks[st]);cout <<"\nMost valuable holding:\n";cout <<*top;return 0;
}