User Tools

Site Tools


notes:cprog:spring2024:projects:cppx

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:cprog:spring2024:projects:cppx [2024/04/17 03:05] – [Member Functions] hcopp1notes: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:
 +<code>g++ -o script script.cpp</code>
 ====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:
 +
 +<code>
 +// Define the class
 +class MyClass {
 +    // Class members go here
 +};
 +</code>
 ====Member Functions==== ====Member Functions====
  
Line 114: Line 126:
 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 ''delete'' or ''delete[]''. A destructor has the same name as the class and is preceded by a ''~''. Example:.... 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 ''delete'' or ''delete[]''. A destructor has the same name as the class and is preceded by a ''~''. Example:....
   * the destructor for class __String__ is declared: __~String()__   * the destructor for class __String__ is declared: __~String()__
 +<code>
 +class String
 +{
 +    public:
 +        String(const char* ch);  // Declare the constructor
 +        ~String();               // Declare the destructor
 +    private:
 +        char* _text{nullptr};
 +};
 +
 +// Define the destructor.
 +String::~String()
 +{
 +    // Deallocate the memory that was previously reserved for the string.
 +    delete[] _text;
 +}
 +
 +</code>
 ====Member Data==== ====Member Data====
 +
 +A data member in C++ is a non-function member of a class (class, struct, or union).
 +<code>
 +class C {
 +    // In C++, this is a "data member".
 +    int x;
 +};
 +</code>
 +
 ====Access Control==== ====Access Control====
 +
 +Access controls enable you to separate the ''public'' interface of a class from the ''private'' implementation details and the ''protected'' members that are only for use by derived classes.
 +
 +Access control helps prevent you from using objects in ways they weren't intended to be used. This protection is lost when you make explicit type conversions.
 +
 ===Public=== ===Public===
 +
 +Class members declared as ''public'' can be used by any function.
 +
 +For example:
 +
 +<code>
 +class MyClass {
 +public:
 +    int publicMember;
 +};
 +</code>
 +
 ===Private=== ===Private===
 +
 +Class members declared as ''private'' can be used only by member functions and friends (classes or functions) of the class.
 +
 +For example:
 +
 +<code>
 +class MyClass {
 +private:
 +    int privateMember;
 +};
 +</code>
 +
 ===Protected=== ===Protected===
 +
 +Class members declared as ''protected'' can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.
 +
 +For example:
 +
 +<code>
 +class MyClass {
 +protected:
 +    int protectedMember;
 +};
 +</code>
 =====Objects===== =====Objects=====
 +
 +In C++, an object is created from a class.
 +
 +<code>
 +class CLASSNAME {
 +   Public:
 +      int sumthinNumb;
 +}
 +/////////////////////////
 +int main() 
 +{
 +   CLASSNAME objname;
 +   objname.sumthinNumb = 12834589028390458;
 +   cout << objname.sumthinNUmb << "/n";
 +
 +   return 0;
 +}
 +}
 +</code>
  
notes/cprog/spring2024/projects/cppx.1713337522.txt.gz · Last modified: 2024/04/17 03:05 by hcopp1