[WIP]C++ Objects Oriented

Created: 2021/08/11

 

Classes & Objects
 class definition  
class sample {
    int a; // this scope is private
    public:
      
    ...
};
Class Member Functions

- define within the definition of the class

class Sample {
    public:
        void test(int i);
}

void Sample::test(int i) { // scope resolution operator
    ...
}

 

- define out of the definition of the class

class Sample {
    public:
        void test(int i) {
             ...
        }
}

 

 

Class Access Modifiers Each section remains in effect untill either other section labels or the closing right curly brace of the class is seen.
public  
protected

A protected member variable or function is very similar to a private member

but it provided one additional benefit that they can be accessed in child classes which are called derived classes.

private default

 

 

Constructor & Destructor  
constructor

 

class Sample {
    public:
        Sample(...) {
            ...
        }
}

 

 

destructor

 

class Sample {
    public:
        ~Sample(...) {
            ...
        }
}

 

 

 

 

Copy Constructor

 a constructor which create an object by initialising it with an object of the same class, which has been created previously.

is used to:

  • Initialize one object from another of the same type.
  • Copy an object to pass it as an argument to a function.
  • Copy an object to return it from a function.
classname (const classname &obj) {
   // body of constructor
}

 

 

Friend Functions

 A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class.

 Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

  • friend functiones or classes must be declared within the declaration of the class
class Sample {
    public:
        friend void getSomeThing(Sample obj);
}

 

it can also be defined completely within the class

class Sample {
    public:
        frined void getSomeThing(...) {
            ...
        }
}

 

 

Inline Functions

 C++ inline function is powerful concept that is commonly used with classes.

 If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

  • A function definition in a class definition is an inline function definition, even without the use of the inline specifier.

 

This Pointer

Every object in C++ has access to its own address through an important pointer called this pointer.

  • The this pointer is an implicit parameter to all member functions. Therefore, 
    inside a member function, this may be used to refer to the invoking object. 
  • Friend functions do not have a this pointer, because friends are not members of a class.Only member functions have a this pointer.
Pointer to C++ Classes

 A pointer to a C++ class is done exactly the same way as a pointer to a structure.

  • to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures
  • Also as with all pointers, you must initialize the pointer before using it
Static Members of a Class

 We can define class members static using static keyword.

  • A static member is shared by all objects of the class
  • All static data is initialized to zero when the first object is created, if no other initialization is present
  • non-const static data member must be initialized out of line

Static Function Members

  By declaring a function member as static, you make it independent of any particular object of the class

    • A static member function can be called even if no objects of the class exist
    • the static functions are accessed using only the class name and the scope resolution operator ::. 
    • A static member function can only access static data member, other static member functions and any other functions from outside the class.
    • Static member functions have a class scope and they do not have access to the this pointer of the class.   

 

 

   
Inheritance
Base and Derived Classes

 

class derived-class: access baseA, access baseB....

 

access-specifier

Where access-specifier is one of public, protected, or private

private by default(if not specified)

base-class base-class is the name of a previously defined class

 

A derived class inherits all base class methods with the following exceptions

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.

A derived class can access all the non-private members of its base class. 

  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

 

Type of Inheritance

We hardly use protected or private inheritance, but public inheritance is commonly used

Access Modifier Base Classes Derived Classes
public

public

public

protected

protected

protected

public

protected

protected

protected

private

public

private

protected

private

  

   
Overloading
 

 C++ allows you to specify more than one definition for a function name or an operator in the same scope,

 which is called function overloading and operator overloading respectively.

 Function Overloading in C++

 

  •  The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
  •  You cannot overload function declarations that differ only by return type.
void sample(int a) {...}
void sample(double a) {...}

 

 

 Operators Overloading in C++

Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined.

Like any other function, an overloaded operator has a return type and a parameter list.

Sample operator+(const Sample& right);

 

In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows

Sample operator+(const Sample& left, const Sample& right);

 

 

   
   
   
   
   
   
Polymorphism
   
   
   
   
   
   
   
   
   
Abstraction
   
   
   
   
   
   
   
   
   
Encapsulation
   
   
   
   
   
   
   
   
   
Interfaces
   
   
   
   
   
   
   
   
   
posted @ 2021-08-11 01:36  懒虫哥哥  阅读(33)  评论(0编辑  收藏  举报