소스 검색

add pointer

Zhilong Li 4 년 전
부모
커밋
7e10b44278
6개의 변경된 파일101개의 추가작업 그리고 21개의 파일을 삭제
  1. 2 2
      CMakeLists.txt
  2. BIN
      include/.try_class.hpp.swp
  3. 58 16
      include/try_class.hpp
  4. 0 0
      include/try_pointer.hpp
  5. 21 3
      main.cpp
  6. 20 0
      try_pointer.cpp

+ 2 - 2
CMakeLists.txt

@@ -8,7 +8,7 @@ include_directories(include)
 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../lib)
 add_library(functions SHARED include/functions.cpp)
 set_target_properties(functions PROPERTIES LINKER_LANGUAGE CXX)
-# link_directories(lib)
-# link_libraries(functions)
+# link_directories(lib) link_libraries(functions)
 add_executable(CppYoutube main.cpp)
+add_executable(try_pointer try_pointer.cpp)
 target_link_libraries(CppYoutube functions)

BIN
include/.try_class.hpp.swp


+ 58 - 16
include/try_class.hpp

@@ -1,21 +1,24 @@
-#include<string>
-#include<list>
-#include<iostream>
+#include <iostream>
+#include <list>
+#include <string>
 
 class YoutubeChannel
 {
     private:
         std::string name;
-        std::string ownerName;
         int subscribersCount;
         std::list<std::string> videoList;
 
+    protected:
+        std::string ownerName;
+        int contentQuality=0;
+
     public:
-        YoutubeChannel(std::string _name, std::string _owner, int _value)
+        YoutubeChannel(std::string _name, std::string _owner)
         {
             name = _name;
             ownerName = _owner;
-            subscribersCount = _value;
+            subscribersCount = 0;
         }
 
         void subscribe()
@@ -25,11 +28,11 @@ class YoutubeChannel
 
         void unsubscribe()
         {
-            if (subscribersCount>0)
+            if (subscribersCount > 0)
                 subscribersCount--;
             else
             {
-                std::cout<<"Can't unsubscribe this channel"<<std::endl;
+                std::cout << "Can't unsubscribe this channel" << std::endl;
             }
         }
 
@@ -38,17 +41,56 @@ class YoutubeChannel
             videoList.push_back(v);
         }
 
+        void qualityCheck()
+        {
+            std::cout<<contentQuality<<std::endl;
+            if(contentQuality<5)
+            {
+                std::cout<<"The quality of "<<name<<" channel is bad."<<std::endl;
+            }
+            else
+            {
+                std::cout<< name << " has great quality!"<<std::endl;
+            }
+
+        }
         void show_info()
         {
-            std::cout<<"**************************"<<std::endl;
-            std::cout<<"Name: "<<name<<std::endl;
-            std::cout<<"Owner: "<<ownerName<<std::endl;
-            std::cout<<"Subscribers: "<<subscribersCount<<std::endl;
-            std::cout<<"Videos:"<<std::endl;
-            for (std::string _value:videoList)
+            std::cout << "**************************" << std::endl;
+            std::cout << "Name: " << name << std::endl;
+            std::cout << "Owner: " << ownerName << std::endl;
+            std::cout << "Subscribers: " << subscribersCount << std::endl;
+            std::cout << "Videos:" << std::endl;
+            for (std::string _value : videoList)
             {
-                std::cout<<"\t"<< _value <<std::endl;
+                std::cout << "\t" << _value << std::endl;
             }
-            std::cout<<"**************************"<<std::endl;
+            std::cout << "**************************" << std::endl;
+        }
+};
+
+class CookingYoutubeChannel : public YoutubeChannel
+{
+    public:
+        CookingYoutubeChannel(std::string name, std::string ownerName) : YoutubeChannel(name, ownerName) {}
+
+        void practice()
+        {
+            std::cout << ownerName << " is practicing cooking..." << std::endl;
+            std::cout << ownerName << "'s channel has gained 1 content quality point." << std::endl;
+            contentQuality++;
+        }
+};
+
+class SingingYoutubeChannel : public YoutubeChannel
+{
+    public:
+        SingingYoutubeChannel(std::string name, std::string ownerName) : YoutubeChannel(name, ownerName) {}
+
+        void practice()
+        {
+            std::cout << ownerName <<" is practicing cooking..." << std::endl;
+            std::cout << ownerName << "'s channel has gained 1 content quality point." << std::endl;
+            contentQuality++;
         }
 };

+ 0 - 0
include/try_pointer.hpp


+ 21 - 3
main.cpp

@@ -28,20 +28,38 @@ int main()
     ///////////////
     // Trying Class
     //
-    YoutubeChannel gg("C++ tutorial", "Lzl", 0);
+    YoutubeChannel gg("C++ tutorial", "Lzl");
     gg.pub_video("C++ for beginners 1");
     gg.pub_video("C++ for beginners 2");
     gg.show_info();
-    for (int subs=0; subs<98; subs++)
+    for (int subs = 0; subs < 98; subs++)
     {
         gg.subscribe();
     }
     gg.show_info();
-    for (int unsubs=0; unsubs<15; unsubs++)
+    for (int unsubs = 0; unsubs < 15; unsubs++)
     {
         gg.unsubscribe();
     }
     gg.show_info();
 
+    CookingYoutubeChannel gg2("How to cook cpp", "L-zl");
+    gg2.pub_video("Cooking 123");
+    gg2.pub_video("Apple pie");
+    // gg2.show_info();
+    gg2.practice();
+    gg2.practice();
+    gg2.practice();
+    gg2.practice();
+    gg2.practice();
+    gg2.practice();
+    // gg2.qualityCheck();
+    SingingYoutubeChannel singer("Amy's sining", "Amy");
+
+    // Using pointers to invoke functions
+    YoutubeChannel * yt1=& gg2;
+    YoutubeChannel * yt2=& singer;
+    yt1->qualityCheck();
+    yt2->qualityCheck();
     return 0;
 }

+ 20 - 0
try_pointer.cpp

@@ -0,0 +1,20 @@
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+    int n = 5;
+    cout << &n << endl;
+
+    int *ptr = &n;
+    cout << ptr << endl;
+    cout << *ptr << endl;
+    *ptr = 10;
+    cout << ptr << endl;
+    cout << *ptr << endl;
+    // the value of n also changed
+    cout << n << endl;
+
+    return 0;
+}