###### tags: `程設筆記`
{%hackmd theme-dark %}
# Chaper 9 Struct, Class
## Class
Reference:
http://ettsengnote.blogspot.com/2016/03/cclasses-and-objects-part-ii.html
http://ettsengnote.blogspot.com/2016/03/cclasses-and-objects-part-ii.html
* What is class?
* Class is a template with Data members and Methods. A instance generated by a template is called an "object".
* public, private and protected
Reference:https://ithelp.ithome.com.tw/articles/10225135
* Public => Accessible to everone.
* Private => Only accessible to the "Class".
* Protected => Only accessible to the "class" and its "subclass".

* ".", "->" and "::"
Reference: https://www.alexleo.click/cc-%E7%AE%AD%E9%A0%AD-%E3%80%81%E9%BB%9E-%E3%80%81%E9%9B%99%E5%86%92%E8%99%9F-%E7%94%A8%E6%B3%95/
Ex1:
```c++=
#include <iostream>
#include <string>
using namespace std;
class Say_sth{
public:
void input(string name, string words);
void output();
private:
string name;
string words;
};
void swap(Say_sth &a, Say_sth &b){
Say_sth temp=a;
a=b;
b=temp;
}
void Say_sth::input(string name, string words){
this->name=name;
this->words=words;
}
void Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
}
int main(){
Say_sth say[2];
string name, words;
for(int i=0; i<2; i++){
getline(cin, name);
getline(cin, words);
say[i].input(name, words);
}
swap(say[0], say[1]);
for(int i=0; i<2; i++){
say[i].output();
}
}
```
Ex2:
```c++=
#include <iostream>
using namespace std;
class Count{
public:
inline void set(int value){
this->value=value;
}
inline void print(){
cout<<value<<"\n";
}
private:
int value;
};
int main(){
Count counter;
Count *counterPtr=&counter;
Count counterRef=counter;
counter.set(1);
counter.print();
counterRef.set(2);
counterRef.print();
counterPtr->set(3);
counterPtr->print();
}
```
* Writing Header file(".h")
Ex:
"Say_sth.h":
```c++=
#ifndef TIME_H
#define TIME_H
#include <iostream>
#include <string>
using namespace std;
class Say_sth{
public:
Say_sth();
void input(string name, string words);
void output();
private:
string name;
string words;
};
#endif
```
Say_sth.cpp:
```c++=
#include <iostream>
#include <string>
#include "Say_sth.h"
using namespace std;
Say_sth(){
name=words="";
}
void swap(Say_sth &a, Say_sth &b){
Say_sth temp=a;
a=b;
b=temp;
}
void Say_sth::input(string name, string words){
this->name=name;
this->words=words;
}
void Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
}
```
Say_sth_main.cpp:
```c++=
#include <iostream>
#include "Say_sth.h"
using namespace std;
int main(){
Say_sth say[2];
string name, words;
for(int i=0; i<2; i++){
getline(cin, name);
getline(cin, words);
say[i].input(name, words);
}
swap(say[0], say[1]);
for(int i=0; i<2; i++){
say[i].output();
}
}
```
Result:

* Constructor and Destructor
Reference: https://www.codementor.io/@supernerdd7/constructor-and-destructor-in-c-1r8kkogm6j
* Constant and Class
* To specify an object is not modifiable.
* A const member function is specified as const bothin its prototype and in its definition.
* Composition: Objects as Members of Classes
Reference:https://medium.com/@pumbaawithmask/%E4%B8%80%E4%BA%9B%E7%AD%86%E8%A8%98-c-98cad9874dd3
* The constructor of one class will automatically create a default constructor(default copy constructor) to copy an object when needed.
Ex:
```c++=
#include <iostream>
using namespace std;
class Name{
public:
Name(string fn="F_Name", string ln="L_name");
void setName(string f_name, string l_name);
void const output();
private:
string f_name;
string l_name;
};
Name::Name(string fn, string ln)
:f_name(fn),l_name(ln)
{
}
void Name::setName(string f_name, string l_name){
this->f_name=f_name;
this->l_name=l_name;
}
void const Name::output(){
cout<<f_name<<" "<<l_name;
}
class Say_sth{
public:
Say_sth(Name &name, string w="Helloworld");
void input(Name &name, string words);
void const output();
private:
Name test;
string words;
};
Say_sth::Say_sth(Name &name, string w)
:test(name),words(w)
{
}
void Say_sth::input(Name &name, string words){
this->test=name;
this->words=words;
}
void const Say_sth::output(){
test.output();
cout<<" said "<<words<<"\n";
}
int main(){
Name name, name2;
name.setName("Ryan", "Lin");
name2.setName("Bryan", "Lin");
Say_sth say(name);
say.output();
say.input(name2,"HaHaPiyan");
say.output();
}
```
* Initializer(Constructor)
Ex:

* Friend Function
* Defined outside public or private.
* Have the access to public and private members.
Ex:
```c++=
#include <iostream>
using namespace std;
class Say_sth{
friend void input(Say_sth &say_sth, string name, string words);
public:
Say_sth(string n="Name", string w="Helloworld");
void const output();
private:
string name;
string words;
};
Say_sth::Say_sth(string n, string w)
:name(n),
words(w)
{
}
void input(Say_sth &say_sth, string n, string w){
say_sth.name=n;
say_sth.words=w;
}
void const Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
}
int main(){
Say_sth test;
input(test, "Ryan", "HaHaPiyan");
test.output();
}
```
* Static Member Function/Data
* A static can only access to a static data.
```c++=
#include <iostream>
using namespace std;
class Say_sth{
public:
Say_sth(string n="Name", string w="Helloworld");
static int getCount();
void input(string name, string words);
void const output();
private:
string name;
string words;
static int count;
};
int Say_sth::count=0; //Only declaring the value at global scope is allowed.
int Say_sth::getCount(){
return count;
}
Say_sth::Say_sth(string n, string w)
:name(n),
words(w)
{
}
void Say_sth::input(string name, string words){
this->name=name;
this->words=words;
count++;
}
void const Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
}
int main(){
Say_sth test;
string name, words;
while(getline(cin,name)){
getline(cin,words);
test.input(name, words);
test.output();
cout<<Say_sth::getCount()<<"\n";
}
}
```
Result:

* this->
* Usually used when one variable is the same as the member of a class.
* About return *thih and return this.

Ex1(same name):

Ex2(cascaded member-function called):
```c++=
#include <iostream>
using namespace std;
class Say_sth{
public:
Say_sth(string n="Name", string w="Helloworld");
Say_sth &setName(string name);
Say_sth &setWords(string words);
void const output();
private:
string name;
string words;
static int count;
};
Say_sth::Say_sth(string n, string w)
:name(n),
words(w)
{
}
Say_sth &Say_sth::setName(string name){
this->name=name;
return *this;
}
Say_sth &Say_sth::setWords(string words){
this->words=words;
return *this;
}
void const Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
}
int main(){
Say_sth test;
string name, words;
while(getline(cin,name)){
getline(cin,words);
test.setName(name).setWords(words);
test.output();
}
}
```
Ex3:
```c++=
#include <iostream>
using namespace std;
class Say_sth{
public:
Say_sth(string n="Name", string w="Helloworld");
void input(string name, string words);
void const output();
private:
string name;
string words;
};
Say_sth::Say_sth(string n, string w)
:name(n),
words(w)
{
}
void Say_sth::input(string name, string words){
this->name=name;
this->words=words;
}
void const Say_sth::output(){
cout<<name<<" said: "<<words<<"\n";
cout<<this->name<<" said: "<<this->words<<"\n";
cout<<(*this).name<<" said: "<<(*this).words<<"\n";
//The code above are the same.
}
int main(){
Say_sth test;
test.input("Ryan", "HaHaPiyan");
test.output();
}
```
Result:

* Operator Overloading
* Operators that can be overloaded

* Operators that cannot be overloaded

Ex:
```c++=
#include <iostream>
using namespace std;
class Complex{
public:
Complex(double real=0, double img=0);
Complex operator+(const Complex &a);
Complex operator-(const Complex &a);
void Display();
private:
double Real;
double Img;
};
Complex::Complex(double real, double img)
:Real(real),Img(img)
{
}
Complex Complex::operator+(const Complex &a){
return Complex(Real+a.Real, Img+a.Img);
}
Complex Complex::operator-(const Complex &a){
return Complex(Real-a.Real, Img-a.Img);
}
void Complex::Display(){
cout<<"Real: "<<Real<<"\n";
cout<<"Img: "<<Img<<"\n";
}
int main(){
Complex a(1, 2), b(3, 4), c;
c=a+b;
c.Display();
c=a-b;
c.Display();
}
```
## Struct
* Basic Application
Ex:
```c++=
#include <iostream>
#include <string>
using namespace std;
struct Client_data{
string name;
int account_id, balance;
};
int balance_addition(Client_data a, Client_data b){
return a.balance+b.balance;
}
int main(){
Client_data Client, Client_2;
Client.name="Ryan";
Client.account_id=1;
Client.balance=100;
Client_2=Client; //copy all data from Client to Client_2
cout<<"Client: "<<Client.account_id<<" "<<Client.name<<" "<<Client.balance<<"\n";
cout<<"Client_2: "<<Client_2.account_id<<" "<<Client_2.name<<" "<<Client_2.balance<<"\n";
cout<<"Addition of Client.balance and Client_2.balance:"<<balance_addition(Client, Client_2)<<"\n";
}
```
* Pointers for a Structure
Ex:
```c++=
#include <iostream>
#include <string>
using namespace std;
struct Complex{
double real;
double img;
};
int main(){
Complex a, b;
Complex *p, *q;
a.real=1;
a.img=2;
p=&a;
cout<<p->img<<"\n";
cout<<p->real<<"\n";
cout<<(*p).img<<"\n";
cout<<(*p).real<<"\n";
q=&b;
q->real=p->img+p->real;
cout<<q->real<<"\n";
cout<<b.real<<"\n";
}
```