8.22.2017

08.22 C++

#include <iostream>
#include <string>

using namespace std;

class drink {
private:
        string name;
        int number, price;
public:
        drink() {
                name = "이름";
                number = 6;
                price = 10;
        }
        void insert() {
                cout << "음료수 이름을 입력하시오." << endl;
                cin >> name;
                cout << "음료수 개수를 입력하시오." << endl;
                cin >> number;
                cout << "음료수 가격을 입력하시오." << endl;
                cin >> price;
        }
        string returnname() {
                return name;
        }
        void printall() {
                cout << "음료수 명 : " << name << ", 음료수 개수 : " << number << ", 음료수 가격 : " << price << endl;
        }
         ~drink() {
        }
};

void main() {
        drink* d[8];

        string name;
        int input = 0, i = 0;
        while (input != 4) {
                cout << "[menu]\n1.물품추가 2.삭제 3.출력 4.종료\n";
                cin >> input;
                if (input > 4) {
                        cout << "1 ~ 4 사이의 수를 입력해주세요. " << endl;
                }
                else if (input == 1) {
                        d[i] = new drink();
                        d[i]->insert();
                        i++;
                }
                else if (input == 2) {
                        cout << "삭제할 음료수는?" << endl;
                        cin >> name;
                        for (int j = 0; j < i; j++) {
                                if (name == d[j]->returnname()) {
                                        delete d[j];
                                        for (int p = 0; p < i-1; p++) {
                                                d[p] = d[p + 1];
                                        } i--;
                                        break;
                                }
                        }
                }
                else if (input == 3) {
                        for (int k = 0; k < i; k++) {
                                d[k]->printall();
                        }
                }
        }
}

======================================================================================

#include <iostream>
#include <string>

using namespace std;

class Human {
public:
        string name;
        int age, height, weight;
        void show() {
                cout << "이름은 " << name << "이고, 나이는 " << age << endl;
        }
};

class student :public Human {
public:
        int k, m, e;
        void input() {
                cout << "국어점수를 입력하시오." << endl;
                cin >> k;
                cout << "수학점수를 입력하시오." << endl;
                cin >> m;
                cout << "영어점수를 입력하시오." << endl;
                cin >> e;
 }
        void printout() {
                show();
                cout << "국어 점수는 " << k << ", 수학점수는" << m << "영어점수는 " << e << "입니다." << endl;
        }
};

class prof :public Human {
public:
        int num;
        void input() {
                cout << "교실 번호를 입력하시오." << endl;
                cin >> num;
        }
        void printout() {
                show();
                cout << "교실 번호는 " << num << "입니다." << endl;
        }
};

void main() {
        student s1;
        prof p1;
        s1.name = "길동이";
        s1.age = 24;
        s1.input();
        s1.printout();

        p1.name = "김선생";
        p1.age = 77;
        p1.input();
        p1.printout();
}

====================================================================================

#include <iostream>

using namespace std;

class Test {
private :
        int a, b;
public:
        Test(int c, int d) {
                a = c;
                b = d;
        }
        void show() {
                cout << a << " , " << b << endl;
        }
};

class child :public Test {
        int k;
public:
        child(int i, int b, int c) : Test (i, b) {
                k = c;
        }
        void show_info() {
                show();
                        cout << k << endl;
        }
};

void main() {
        child c1(10, 20, 30);
        c1.show_info();
}

===================================================================================

#include <iostream>

using namespace std;

class Test {
public:
        Test() {
                cout <<  "parent 소멸자 \n";
        }
        ~Test() {
                cout <<  "parent 소멸자 \n";
        }
};
class Child : public Test {
public:
        Child() {
                cout <<  "child 생성자 \n";
        }
        ~Child() {
                cout << "child 소멸자 \n";
        }
};

void main() {
        Child c1;
}

=================================================================================

#include <iostream>

using namespace std;

class Test {
        int num1, num2, num3;

public:

        void init() {
                num1 = 10;
                num2 = 0;
                num3 = 0;
        }
        void init(int k,int p) {
                num1 = num2 = 20;
                num3 = 0;
        }
        void init(int k, char p) {
                num1 = num2 = num3 = 30;
        }
        void show() {
                cout << num1 << " , " << num2 << " , " << num3 << endl;
        }
};

void main() {
        Test t;
        t.init(1, 'l');
        t.show();
}

=================================================================================

#include <iostream>
#include <string>

using namespace std;

class Computer {
private:
        string name;
        int number, price;
public:
        Computer(string abc) {
                name = abc;
                number = 0;
                price = 0;
        }
        Computer(string a, int b) {
                name = a;
                price = b;
                number = 0;
        }
        Computer(string a, int b, int c) {
                name = a;
                price = b;
                number = c;
        }
        void p() {
                cout << "The name is " << name << ", the price is " << price << ", the number is " << number << endl;
        }
};

void main() {
        Computer c1("abc");
        Computer c2("myblog ", 20);
        Computer c3("akljsflqjwfeoijasf", 24, 90);
        c1.p();
        c2.p();
        c3.p();
}

댓글 없음:

댓글 쓰기