OOP summary

时间:2021-06-05 22:06:28   收藏:0   阅读:41

Preliminaries

Linux Basics

Command-line Arguments

Access argv and envp in the main routine

int main(int argc, char **argv, char **envp)

Compilation and multiple-file projects

options for g++

Declaration

A function (函数) or variable (变量) can be defined only once, but can be declared many times.

Include Header Files

Makefile

Separate Compilation

make and Makefile

A tool called make is introduced to manage the compilation process.

test.exe: product.cpp sum.cpp main.cpp functions.h
	g++ product.cpp sum.cpp main.cpp -o test.exe

Dependency rule: if test.exe does not exist or is older than any of the files after the colon(??, the following command (g++ …) will be executed.

an example:

test.exe: product.o sum.o main.o
	g++ product.o sum.o main.o -o test.exe
product.o: product.cpp functions.h
	g++ -c product.cpp -o product.o
sum.o: sum.cpp functions.h
	g++ -c sum.cpp -o sum.o
main.o: main.cpp functions.h
	g++ -c main.cpp -o main.o

More on Makefile

There are tools for automatically generating the makefile, such as cmake.

Library

Static library (Linux)

A particular file of archive format (with .a suffix), which is a collection of concatenated relocatable object files, with a header describing the size and location of each object file.

g++ -static main.o test.a -o test.out
g++ -static main.o -L. -ltest -o test.out

-static: tells the linker to build a fully linked executable object file without any further linking at load time

Shared Library

An object module (with .so suffix) loaded at an arbitrary memory address (内存地址) and linked with a program in memory, at either run time (运行期) or load time (启动期). This is called dynamic linking (动态链接)

g++ -c -fPIC sum.cxx -o sum.o
g++ -shared -o libtest.so product.o sum.o

Class

variables and function are encapsulated (封装) into a structure called class.

Key features of OOP:

Object & Abstraction

Member Variable and Member Function

class Bird{
	public:
		void fly();
		void sing();
		void eat(); //Member Functions
	private:
		std::string name_;
		std::string color_;
		float weight_;
		float height_; //Member Variables
};

Access Control to Class Members

Public Members

Private Members

Protected Members

Definition of Member Functions

Member function

Inline function

Functions less than 10 lines can be declared inline to improve efficiency

#ifndef BIRD_H_
#define BIRD_H_ //Preprocessor directive
#include <iostream>
#include <string>
class Bird {
	public:
		void setSpeed(float speed) {
			speed_ = speed;
		}//inline functions
	private:
		float speed_;
};
#endif
#include "bird.h"
using namespace std;
void Bird::fly(float time) {
	float distance = speed_ * time;
	cout << name_ << " flies distance: " << distance << endl;
}

Encapsulation

this Pointer

class Bird {
	public:
		void fly(float time);
		Bird* setName(char *name) {
			name_ = name;
			return this;
		}
	private:
		std::string name_;
};
//User program:
Bird b;
b.setName(“Eagle")‐>fly(10.0);

Constructor (构造函数)

Lifetime (生命期) or scope (作用域) of objects

Constructor (ctor, 构造函数) and destructor (dtor, 析构函数) are introduced for better SAFETY (安全性)

Why constructor is necessary?

  • Constructor provides a place to ensure certain task be executed, such as member initialization, along with object creation (对象创建时进行初始化)

  • Avoids the careless/wrong usage of a class

Constructor is automatically called by the compiler when the object is being created for definition of an object:

ClassA a; //Stack object
ClassA *pA = new ClassA; //Heap object 

Default Constructor

a special constructor without function arguments (above. Constructor 1)

When no constructor is defined in a class, the compiler will synthesize (合成) a default constructor, called a default default constructor

Using default keyword for Default Constructor

The use of the default keyword is preferred in modern C++ code

class Tree {
		int height_{0};
	public:
		Tree() = default; //equals to Tree(){}
};

Member Initializer List

class Tree {
		int height_ {0}, year_ {0};
	public:
		Tree(int h, int y) : year_{y}, height_{h} { }
};

enjoys better efficiency

Keyword explicit

Single-parameter constructor enables the implicit type conversion (隐式类型转换)

To forbid such conversions, use keyword explicit.

class Tree {
		int height_{0};
	public:
		Tree(int h):height_{h} {}
	//explicit Tree(int h):height_{h} {}
}; //…
void foo(Tree t){
	//…
}
foo(5); //works if no explicit keyword, error if explicit is used

Delegating Constructor

Depends on other constructors for the initialization

class Tree {
		int height_, size_;
	public:
		Tree(int h) : height_{h} { }
		Tree() : Tree {h} { size_ = 0; }//Delegating constructor
};

Destructor (析构函数)

Clean up the resource (memory) occupied by the object. A special function called destructor(prepare-to-die) is introduced for cleanup.

Destructor is automatically called by compiler when:

DON’T explicitly call (显式调用) the destructor unless necessary.

pA->~ClassA(); // works but NOT recommended 

When is a destructor necessary?

  • To free (释放) member variables allocated on the heap (堆)
  • Free other occupied resources (socket, printer, etc.)

Default Keyword

the same as previous

Stack vs. Heap Variables

原文:https://www.cnblogs.com/zjp-shadow/p/14853809.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!