notes:cprog:spring2024:projects:cppx
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:cprog:spring2024:projects:cppx [2024/04/17 02:21] – [Member Functions] hcopp1 | notes:cprog:spring2024:projects:cppx [2024/04/24 12:23] (current) – [Classes] amelvil2 | ||
---|---|---|---|
Line 1: | Line 1: | ||
======CPPX====== | ======CPPX====== | ||
=====C++ compiler===== | =====C++ compiler===== | ||
+ | To compile a C++ script, you can use g++ -o (new name) (uncompiled name).cpp | ||
+ | |||
+ | For example, to compile a script called script.cpp: | ||
+ | < | ||
====common file extensions==== | ====common file extensions==== | ||
Line 25: | Line 29: | ||
* The public members form an interface to the class and are accessible outside the class. | * The public members form an interface to the class and are accessible outside the class. | ||
+ | For example: | ||
+ | |||
+ | < | ||
+ | // Define the class | ||
+ | class MyClass { | ||
+ | // Class members go here | ||
+ | }; | ||
+ | </ | ||
====Member Functions==== | ====Member Functions==== | ||
Line 30: | Line 42: | ||
* if its defined outside the class, use the scope resolution ''::'' | * if its defined outside the class, use the scope resolution ''::'' | ||
+ | defined inside: | ||
+ | < | ||
+ | class Cube | ||
+ | { | ||
+ | public: | ||
+ | int side; | ||
+ | int getVolume() | ||
+ | { | ||
+ | return side*side*side; | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | defined outside: | ||
+ | < | ||
+ | class Cube | ||
+ | { | ||
+ | public: | ||
+ | int side; | ||
+ | int getVolume(); | ||
+ | } | ||
+ | |||
+ | int Cube :: getVolume() | ||
+ | { | ||
+ | return side*side*side; | ||
+ | } | ||
+ | </ | ||
===Constructor=== | ===Constructor=== | ||
+ | |||
+ | A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created. | ||
+ | |||
+ | default constructor | ||
+ | < | ||
+ | class Line | ||
+ | { | ||
+ | | ||
+ | int size; | ||
+ | |||
+ | Line() | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | }; | ||
+ | ///////////// | ||
+ | int main() | ||
+ | { | ||
+ | // | ||
+ | Line l; | ||
+ | } | ||
+ | </ | ||
+ | parameter constructor | ||
+ | < | ||
+ | class ABC | ||
+ | { | ||
+ | | ||
+ | int x,y; | ||
+ | | ||
+ | ABC () // | ||
+ | { | ||
+ | x = y = 0; | ||
+ | } | ||
+ | ABC(int a) // | ||
+ | { | ||
+ | x = y = a; | ||
+ | } | ||
+ | ABC(int a,int b) // | ||
+ | { | ||
+ | x = a; | ||
+ | y = b; | ||
+ | } | ||
+ | }; | ||
+ | ///////////////// | ||
+ | int main() | ||
+ | { | ||
+ | ABC cc1; // | ||
+ | ABC cc2(10); // | ||
+ | ABC cc3(10,20); // | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
===Destructor=== | ===Destructor=== | ||
+ | |||
+ | A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to '' | ||
+ | * the destructor for class __String__ is declared: __~String()__ | ||
+ | < | ||
+ | class String | ||
+ | { | ||
+ | public: | ||
+ | String(const char* ch); // Declare the constructor | ||
+ | ~String(); | ||
+ | private: | ||
+ | char* _text{nullptr}; | ||
+ | }; | ||
+ | |||
+ | // Define the destructor. | ||
+ | String:: | ||
+ | { | ||
+ | // Deallocate the memory that was previously reserved for the string. | ||
+ | delete[] _text; | ||
+ | } | ||
+ | |||
+ | </ | ||
====Member Data==== | ====Member Data==== | ||
+ | |||
+ | A data member in C++ is a non-function member of a class (class, struct, or union). | ||
+ | < | ||
+ | class C { | ||
+ | // In C++, this is a "data member" | ||
+ | int x; | ||
+ | }; | ||
+ | </ | ||
+ | |||
====Access Control==== | ====Access Control==== | ||
+ | |||
+ | Access controls enable you to separate the '' | ||
+ | |||
+ | Access control helps prevent you from using objects in ways they weren' | ||
+ | |||
===Public=== | ===Public=== | ||
+ | |||
+ | Class members declared as '' | ||
+ | |||
+ | For example: | ||
+ | |||
+ | < | ||
+ | class MyClass { | ||
+ | public: | ||
+ | int publicMember; | ||
+ | }; | ||
+ | </ | ||
+ | |||
===Private=== | ===Private=== | ||
+ | |||
+ | Class members declared as '' | ||
+ | |||
+ | For example: | ||
+ | |||
+ | < | ||
+ | class MyClass { | ||
+ | private: | ||
+ | int privateMember; | ||
+ | }; | ||
+ | </ | ||
+ | |||
===Protected=== | ===Protected=== | ||
+ | |||
+ | Class members declared as '' | ||
+ | |||
+ | For example: | ||
+ | |||
+ | < | ||
+ | class MyClass { | ||
+ | protected: | ||
+ | int protectedMember; | ||
+ | }; | ||
+ | </ | ||
=====Objects===== | =====Objects===== | ||
+ | |||
+ | In C++, an object is created from a class. | ||
+ | |||
+ | < | ||
+ | class CLASSNAME { | ||
+ | | ||
+ | int sumthinNumb; | ||
+ | } | ||
+ | ///////////////////////// | ||
+ | int main() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | cout << objname.sumthinNUmb << "/ | ||
+ | |||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
notes/cprog/spring2024/projects/cppx.1713334860.txt.gz · Last modified: 2024/04/17 02:21 by hcopp1