C++学习(1)

好困,不过在睡觉之前还是写一篇学习心得再睡。

虽然早就结束了这门课,可是那个时候不知道是老师讲的太糟糕还是我根本没有听,其实C++学的挺烂的,平时ACM就用用简单的一些基本语句就可以了。但是深知这远远不够,所以我今天重新翻开教科书还有《C++ primer》想从今天起,有空好好把C++也好好学学~小弟不才,大牛们请绕过。

样例也比较简单,请勿吐槽。

1.函数模板:

有点类似与函数的重载,但是是一段通用的代码适用于多种不同的数据类型,可重性很大。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 template <typename a,typename b>
 5 a add(a x,b y)               //返回的类型是 a类型 
 6 {
 7          cout<<"("<<sizeof(a)<<','<<sizeof(b)<<")\t";
 8          return x+y;
 9 } 
10 
11 int add(int x,int y)
12 {
13     cout<<"(int, int)\t";
14     return x+y;
15 }
16 
17 int main()
18 {
19      cout<<add(9,8)<<endl;      //优先绑定重载函数 
20      cout<<add(9.0,8.0)<<endl;
21      cout<<add(9.0,8)<<endl;
22      cout<<add('A','A'-'0')<<endl;
23      cout<<add(double(8),9)<<endl;
24      system("pause");
25      return 0;
26 }

2.宏定义

这个经常使用,这里用例子说个值得注意的地方

 1 #include <iostream>
 2 #define XYZ "this is a test"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     cout<<"XYZ"<<endl;    //输出XYZ
 8     cout<<XYZ<<endl;    //输出this is a test
 9     system("pause");
10     return 0;
11 } 
12 
13 
14 #include <iostream>
15 #define XYZ this is a test    //此处去掉双引号 
16 using namespace std;
17 
18 int main()
19 {
20     cout<<"XYZ"<<endl;                   //输出XYZ 
21     cout<<XYZ<<endl;                     //编译错误 
22     system("pause");
23     return 0;
24 } 

3.类

  1)简单的类:

    

 1 // 模拟始终 (类)
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Clock{
 6       private:
 7               int H,M,S;
 8       public:
 9              void SetTime(int h,int m, int s)
10              {
11                   H=(h>=0 && h<24)?h:0;
12                   M=(m>=0 && m<60)?m:0;
13                   S=(s>=0 && s<60)?s:0;
14              }      
15              
16              void ShowTime()
17              {
18                   cout<<H<<":"<<M<<":"<<S<<endl;
19              }
20 };
21 
22 int main()
23 {
24     Clock MyClock;
25     MyClock.ShowTime();                   //这一行要是放在main()外面输出结果不一样,因为全局变量和局部变量在没有初始化的时候,取初值方式不同 
26     MyClock.SetTime(8,30,30);
27     MyClock.ShowTime();
28     system("pause");
29     return 0;
30 }
31 
32 /*输出结果是:
33 -85893460:-85893460:-85893460
34 8:30:30 
35 */ 

  2)类的成员函数

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Clock{                
 5       private:
 6               int H,M,S;
 7       public:
 8              void SetTime(int h,int m,int s) ;      //成员函数 在类中声明 有利于对该类功能一目了然 
 9              void ShowTime();     
10 };
11 
12 void Clock::SetTime(int h,int m,int s)
13 {
14      H=h,M=m,S=s;
15 }
16 
17 void Clock::ShowTime()        //成员函数
18 {
19      cout<<H<<":"<<M<<":"<<S<<endl;
20 }
21 int main()
22 {
23     Clock MyClock;
24     MyClock.SetTime(8,30,30);
25     MyClock.ShowTime();
26     system("pause");
27     return 0;
28 }

  3)构造函数

 1 //构造函数        由于类的数据成员是private的,所以不可以在外面赋值
 2 //所以通过函数来调用该类的私有成员,这个成员函数就叫做”构造函数“。 
 3 #include <iostream>
 4 using namespace std;
 5 
 6 class Clock{                
 7       private:
 8               int H,M,S;
 9       public:
10              void SetTime(int h,int m,int s)
11              {
12                         H=(h>=0 && h<24)?h:0;
13                        M=(m>=0 && m<60)?m:0;
14                        S=(s>=0 && s<60)?s:0;
15                  }
16       Clock(int h=0,int m=0,int s=0)        //构造函数
17       {
18                H=(h>=0 && h<24)?h:0;
19                M=(m>=0 && m<60)?m:0;
20                S=(s>=0 && s<60)?s:0;
21       }
22       
23       void ShowTime()
24       {
25               cout<<H<<":"<<M<<":"<<S<<endl;
26          }
27 };
28 
29 int main()
30 {
31     Clock MyClock;
32     MyClock.ShowTime();
33     Clock MyClock2(9,30,45);        //有了构造函数,可以在建立对象时直接初始化 
34     MyClock2.ShowTime();
35     Clock MyClock3=Clock(9,30,45);        //也可以这样建立新的对象 
36      MyClock3.ShowTime();
37     system("pause");
38     return 0;
39 }

  4)析构函数

 1 //析构函数2   
 2 //基本字符串类
 3 #include <iostream>
 4 using namespace std;
 5 
 6 class String{
 7       private:
 8                 char *Str;
 9                 int len;
10       public:
11                void ShowStr()
12                {
13                     cout<<"string:"<<Str<<", length:"<<len<<endl;
14                  }
15                  String()
16                  {
17                        len=0;
18                        Str=NULL;
19                }
20                String(const char *p)
21                {
22                   len=strlen(p);
23                   Str=new char[len+1];
24                   strcpy(Str,p);
25                }
26                ~String()          //析构函数
27                {
28                           if(Str!=NULL)            //清理程序 
29                           {
30                               delete [] Str;
31                               Str=NULL;
32                          }
33                  }
34 };
35 
36 int main()
37 {
38      char s[]="ABCDE";
39      String s1(s);
40      String s2("123456");
41      s1.ShowStr();
42      s2.ShowStr();
43      system("pause");
44      return 0;
45 }

  

原文地址:https://www.cnblogs.com/shenshuyang/p/2637296.html