C++ Final Study Guide Chap 14, 15

Chapter 14 - Static Member variables and Copy Constructors
Do you know the difference between a static and a non-static member of a class?
Do you understand the difference between a copy constructor, a default constructor and other constructors?

SG:Static和非static的区别
  1. static和非static的member不能重名
  2. 引用static member的时候不需要创建类的对象 (比如说call一个static的function 的时候,我们不需要创建该类的instance)。
  3. static的member function没有this指针,而且该类的所有对象都可以access这个static variable。
  4. static函数只能调用其他static函数、变量:static member functions do not operate on instance variables
  5. static函数不能为virtual, const, volatile, or const volatile
  6. instance variable有自己的copy,但是static member variables只有一个copy of the member variable in memory。
  7. static的variable要在class definition外面
class Tree{
private:
	static int objCount;
public:
	Tree(){
		objCount++; //objCount increments everytime an object is created
	}
	int getObjCount(){
		return objCount;
	}
};
int Tree::objCount = 0; //如果我们不自己initialize为0,c++也会帮我们initialize为0
  1. static member functions:不可以调用非static的variable。static functions和variables在类的对象被创建之前就存在了。ClassName::staticFunctionName(paramter); (在main中调用static fuctions,::叫scope resolution operator)
Friends of Classes
  1. friend是一种,不是类的member,但是可以access这个类的private数据的方程。A friend function can be a regular stand-alone function, or it can be a member of another class. (In fact, an entire calss can be declared a friend of another class. )
friend ReturnType FunctionName (paramterList)

在Budget.h中定义:(想成为谁的朋友就在谁的h中定义。)
friend void AuxiliaryOffice::addBudget(double, Budget &);

在AuxilirayOffice中:
void AuxiliaryOffice::addBudget(double b, Budget &div){
auxBudget +=b;
div.corpBudget += b; //corpBudget is from Budget class.
}
  1. Make an entire class a friend of another class:
    每个AuxiliaryOffice的function都可以访问Budget的private members。最好不要这么做,最好只让一定要access private members的方程成为friend。
friend class AuxiliaryOffice; 
SG:Copy Constructor和普通Constructor
  1. Copy Constructor: a copy constructor is a special constructor that is called whenever a new obkect is created and initialized with another object’s data.

    公式: ClassName(const ClassName &oldObj) { }
    创建一个新对象的时候用另一个对象的数据。

    关于:“即使你不自己定义这个copy constructor,C++会自动提供给你。“
    如果你的class没有pointer variable没有动态分配的内存,那么default constructor 也可以work(shallow copy)。但是如果有pointer variables and dynamically allocated memory,你必须自己提供copy constructor。

    Parameter必须是一个reference对象(最好加上const keyword,因为我们不需修改pass进来的对象)。
    因为如果一个对象pass by value进入copy constructor,这个copy constructor会创建一个新的copy并且存在parameter之中。当parameter的对象被创建的时候,copy constructor会被call,然后另一个parameter对象又会被创建。一直反复下去,直到内存爆炸。

    Copy constructor被call的三种情况
    第一,When an object is created from another object of the same type(根据一个对象的value复制新对象)。
    第二,当一个对象pass by value进入function的时候。
    第三,当一个对象被return回到function的时候。

StudentTestScores(const StudentTestScores &obj) { 
studentName = obj.studentName; 
numTestScores = obj.numTestScores; 
testScores = new double[numTestScores]; 
for (int i = 0; i < numTestScores; i++)
testScores[i] = obj.testScores[i]; 
}
................................................................
StudentTestScores oneCopy = one; //calling the copy constructor

网上找的例子:https://people.cs.clemson.edu/~rlowe/cs1070/notes/copy_constructor.pdf
2. 普通Constructor

// Constructor
StudentTestScores(string name, int numScores) { 
studentName = name;
createTestScoresArray(numScores); 
}
Aggregation

Aggregation occurs when a class contains an instance of another class. 比如course类中有instructor和textbook这两个类的object作为private variables。在UML中用菱形标识。

Chapter 15 - Inheritance
How to define a derived class
How accessible are the parent’s private, protected and public members to the child class
What is an abstract class and how does it relates to creating objects?
What is a pure virtual function and what does it mean to a class?

Chap 15 Highlights:

Overriding and Redefining:

Overriding:(dynamic bound)
只有virtual的函数可以被override,当我们重新define一个虚拟函数的时候,我们就在override它。
In C++, the difference between overriding and redefining base class functions is that overridden functions are dynamically bound, and redefined functions are statically bound. Only virtual functions can be overridden.

譬如这个就是Redefining:(static bound)
void setScore(double s){
rawScore = s;
GradedActivity ::setScore(rawScore * percentage);}

另外,C++的Override放在signature之后:比如base class中是virtual void functionA(int arg) const,
那么derived class中就是virtual void functionA(int arg) const override

Destructor

When you write a class with a destructor, and that class could potentially become a base class, you should always declare the destructor virtual. This is because the compiler will perform static binding on the destructor if it is not declared virtual.

多态、pure virtual functions
  1. PVF的意义:确定抽象类
    C++是通过拥有Pure Virtual Function来确定这个class是不是抽象类的:
    抽象类没有对象,以及PVF必须被overridden(virtual void showInfo() = 0;)
    (抽象类也可以有concrete methods)

  2. 多态:根据对象类型来决定call什么函数,the ability to take many forms。c++中,多态需要reference或者指针。
    当一个对象pass by value的时候,多态是不可能的。

void displayGrade (const GradedActivity activity){
	cout << activity.getScore() << " " << activity.getLetterGrade();
}
//Polymorphic behavior impossible

正确的用法应该是使用指针:

void displayGrade(const GradedActivity *activity){
	cout << activity->getScore() << " " << activity->getLetterGrade();
}
  1. 静态绑定:在compile的时候决定call哪个函数;动态绑定:根据对象类型决定使用哪个函数。
  2. Virtual Fuctions:(virtual这个关键字只在declaration和prototype里面用,如果你在class外面define这个函数是不要加virtual的。)
virtual char getLetterGrade() const;
指向父类的指针

我们可以assign子类对象的地址给父类的指针。限制是这个指针只能access父类拥有的functions。

BaseClass * bc = new DerivedClass (0,0,0);

不可以直接反向推回来,除非你用static_cast < subclass *>(ptr),比如:

GradedActivity  * gaPtr = new GradedActivity (100);
FinalExam * fePtr = gaPtr; //ERROR!!!

TO FIX THIS, Casting needed:
FinalExam * fePtr = static_cast<FinalExam *>(gaPtr);
override和final

virtual void functionA (int arg) const override {…body…;}
virtual void message () const final; //cannot be overriden anymore

Public, Protected, Private & OOP
class Test : Grade 
//class access specification默认为pivate,即使你不写

Textbook P905

class GradedActivity{

protected:
char letter;
double score;
void determineGrade();

public:
GradedActivity(){
letter = ' ';
score = 0.0;
}

void setScore(double s){
score = s;
determineGrade();
}

double getScore() const{
return score;
}

double getLetterGrade() const{
return letter;
}
};

//Since a curved activity is a graded activity

class CurvedActivity : public GradedActivity{
protected:
double rawScore;
double percentage;

public:
CurvedActivity() : GradedActivity(){
rawScore = 0.0;
percentage = 0.0;
}

void setScore(double s){
rawScore = s;
GradedActivity ::setScore(rawScore * percentage);
}

void setPercentage(double c){
percentage = c;
}

double getPercentage() const{
return percentage;
}

double getRawScore() const{
return rawScore;
}
};

//

Textbook: Starting out with C++

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值