5. OOP: Initialization

Initialization

0. Overview

It a simple concept distinguish: what default constructor, constructor with values, and initializer_list.

1. default < constructor < initializer_list

When we create a class, we are required to write a member function as className() {}. This is default constructor, taking no element, and you could initialize the member variables.

Sometimes, we have constructor with elements, e.g. className(int a, int b) { … }. This is constructor with values, which means object is initialized with certain value passed by user.

Sometimes, we initialize the variable with a list of object, e.g. vector< int> ex = {1,2,3,4};. {1, 2, 3, 4} here is the initializer_list, which means initialize the variable with those numbers.

2. How to sort/find a class

We know, sort and find can be applies on the primitive data type, but how to apply to user-defined class? We need to overloading operator<, operator==.

3. Complete Code:

// initializing data member of a class
// default < constuctor < initializer_list
// operator< usually for sorting
// operator== usually for find
#include <iostream>
#include <list>
#include <vector>
#include <map>

using namespace std;

class ThreeD{
public:
    int ht;
    int wid;
    int dep;

    // default constructor
    ThreeD() {
        ht = wid = dep = 0;
    }
    // know knid of constructor
    ThreeD(int i, int j, int k): ht(10*i), wid(10*j), dep(10*k) {}
    // highest priority
    ThreeD(const initializer_list<int>& I);
    int getVol() const { return ht * wid * dep; }

    bool operator<(const ThreeD& t) const { return getVol() < t.getVol();}
    bool operator==(const ThreeD& t) const{  return getVol() == t.getVol();}
};

// highest priority
ThreeD::ThreeD(const initializer_list<int>& I){
    auto it = I.begin();
    ht = *it * 100;
    it++;
    wid = *it * 100;
    it++;
    dep = *it * 100;
}

class Node{
public:
    int value;
    Node* next;
    Node(){ next = nullptr; }
    Node(int i ):value(i), next(nullptr){}
};


class LinkedList {
public:
    Node* head;
    LinkedList(): head(nullptr){};
    LinkedList(const initializer_list<int>& I );
    void printList();
};
LinkedList::LinkedList(const initializer_list<int>& I ){
    head = nullptr;
    auto it = I.end() - 1;
    while(it != I.begin()-1 ){
        Node* p = new Node(*it);
        p->next = head;
        head = p;
        it--;
    }
}

void LinkedList::printList(){
    Node* p = head;
    while(p){
        cout << p->value ;
        if(p->next)
            cout << "->" ;
        p = p->next;
    }
    cout << endl;

}


ostream& operator<<(ostream& str, const ThreeD& T){
    str << "[" << T.ht << ", " << T.wid << ", " << T.dep << "]";
    return str;
}

int main(){

    int A[5]{1 }; // {1, 0, 0, 0, 0}


    // t1(3,4,5) call the constructor                       ===> [30, 40, 50]
    // however, t2{10, 20, 30} call the initializer_list    ===> [1000, 2000, 3000]
    ThreeD t1(3,4,5), t2{10,20,30}, t3{1,2,6};
    cout << t1 << " " << t2 << " " << t3 << endl;
 
    list<int> L1{7, 4,3,5,1};
    auto it11 = find(L1.begin(), L1.end(), 5);
    for(auto i = L1.begin(); i != L1.end(); i++)
    {
        if (i == it11)
        cout << "|" ;
        cout<< *i << " ";
    }

    L1.sort();
    for(auto i : L1){
        cout << i << " ";
    }
    cout << endl;

    list<ThreeD>  L2{t1, t2, t3};
    //need to overload operator=
    auto it12 = find(L2.begin(), L2.end(), t2);
    
    // we couod sort, because we had override the operator<() of ThreeD
    L2.sort();
    for(auto i : L2) {
        cout << i << " ";
    }
    cout << endl;

    map<int, string> M1{ {25,"Dave"}, {11, "Nancy"}, {14, "Mary"} };
    // find is a member fucntion for map, but not for list or vector
    // in map, find function return a iterator
    auto it13 = M1.find(25);
    
    for(auto it = M1.begin(); it != M1.end(); it++)
    {
        if(it == it13)
        {
            cout << "|" ;
        }
        cout << "["<< (*it).first << "," << it->second << "] ";
    }
    cout << endl;

    for(auto i : M1) {
        cout << i.first << " " << i.second << "     ";
    }
    cout << endl;
    
    map<ThreeD, string> M2{ {t1, "Dave"}, {t2, "Nancy"}, {t3, "Mary"} };
    for(auto i : M2) {
        cout << i.first << " " << i.second << "     ";
    }
    cout << endl;

    LinkedList L5 { 4,5,6,7,8 };
    L5.printList();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值