IT 프로그래밍/객체지향프로그래밍

객체지향프로그래밍 시험 예상 문제 코드

기술1 2024. 6. 22. 21:22
반응형

상속

문제 : 다음 클래스 다이어그램을 참고하여 computer와 notebook 클래스를 작성하시오

 

Computer
- modelName: string
- processor: string
- ramSize: int
- diskSize: int

+ Computer(modelName: string, processor: string, ramSize: int, diskSize: int)
+ displayDetails(): void

Notebook extends Computer
- weight: double
- batteryLife: double

+ Notebook(modelName: string, processor: string, ramSize: int, diskSize: int, weight: double, batteryLife: double)
+ displayDetails(): void

 

#include <iostream>
#include <string>

using namespace std;

class Computer {
protected:
    string modelName;
    string processor;
    int ramSize;
    int diskSize;

public:
    Computer(string modelName, string processor, int ramSize, int diskSize) 
        : modelName(modelName), processor(processor), ramSize(ramSize), diskSize(diskSize) {}

    virtual void displayDetails() {
        cout << "Model: " << modelName << endl;
        cout << "Processor: " << processor << endl;
        cout << "RAM: " << ramSize << " GB" << endl;
        cout << "Disk: " << diskSize << " GB" << endl;
    }
};

class Notebook : public Computer {
private:
    double weight;
    double batteryLife;

public:
    Notebook(string modelName, string processor, int ramSize, int diskSize, double weight, double batteryLife)
        : Computer(modelName, processor, ramSize, diskSize), weight(weight), batteryLife(batteryLife) {}

    void displayDetails() override {
        Computer::displayDetails();
        cout << "Weight: " << weight << " kg" << endl;
        cout << "Battery Life: " << batteryLife << " hours" << endl;
    }
};

 

포인터와 동적 객체 생성

사용자가 입력한 정수 배열을 동적으로 생성하고, 배열의 평균을 계산하는 프로그램을 작성하시오.

 

#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;

    int* arr = new int[n];
    cout << "Enter " << n << " elements: ";
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    double average = static_cast<double>(sum) / n;
    cout << "Average: " << average << endl;

    delete[] arr;
    return 0;
}

 

가상함수와 다형성

문제 : 다음과 같은 Shape 클래스와 이를 상속받은 circle 및 rectangle 클래스를 작성하고, 가상함수를 사용하여 각각의 넓이를 계산하는 프로그램을 작성하시오.

 

Shape
+ area(): double

Circle extends Shape
- radius: double

+ Circle(radius: double)
+ area(): double

Rectangle extends Shape
- width: double
- height: double

+ Rectangle(width: double, height: double)
+ area(): double

 

#include <iostream>

using namespace std;

class Shape {
public:
    virtual double area() const = 0;
};

class Circle : public Shape {
private:
    double radius;

public:
    Circle(double radius) : radius(radius) {}

    double area() const override {
        return 3.14159 * radius * radius;
    }
};

class Rectangle : public Shape {
private:
    double width;
    double height;

public:
    Rectangle(double width, double height) : width(width), height(height) {}

    double area() const override {
        return width * height;
    }
};

int main() {
    Shape* shapes[2];
    shapes[0] = new Circle(5.0);
    shapes[1] = new Rectangle(4.0, 6.0);

    for (int i = 0; i < 2; i++) {
        cout << "Area: " << shapes[i]->area() << endl;
    }

    delete shapes[0];
    delete shapes[1];

    return 0;
}

 

복사 생성자와 할당 연산자

DynamicArray 클래스를 작성하고, 복사 생성자와 할당 연산자를 구현하시오. 이 클래스는 동적으로 정수 배열을 할당하고 관리합니다. 

 

DynamicArray
- size: int
- data: int*

+ DynamicArray(size: int)
+ ~DynamicArray()
+ DynamicArray(const DynamicArray& other)
+ DynamicArray& operator=(const DynamicArray& other)
+ print(): void

 

#include <iostream>

using namespace std;

class DynamicArray {
private:
    int size;
    int* data;

public:
    DynamicArray(int size) : size(size) {
        data = new int[size];
    }

    ~DynamicArray() {
        delete[] data;
    }

    DynamicArray(const DynamicArray& other) : size(other.size) {
        data = new int[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }

    DynamicArray& operator=(const DynamicArray& other) {
        if (this == &other) {
            return *this;
        }

        delete[] data;

        size = other.size;
        data = new int[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }

        return *this;
    }

    void print() const {
        for (int i = 0; i < size; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    DynamicArray arr1(5);
    for (int i = 0; i < 5; i++) {
        arr1.data[i] = i + 1;
    }

    DynamicArray arr2 = arr1;
    DynamicArray arr3(3);
    arr3 = arr1;

    arr1.print();
    arr2.print();
    arr3.print();

    return 0;
}

 

순수 가상 인터페이스

Drawable 인터페이스를 정의하고 이를 구현하는 square 클래스를 작성하시오

 

Drawable
+ draw(): void

Square implements Drawable
- side: double

+ Square(side: double)
+ draw(): void

 

#include <iostream>

using namespace std;

class Drawable {
public:
    virtual void draw() const = 0;
};

class Square : public Drawable {
private:
    double side;

public:
    Square(double side) : side(side) {}

    void draw() const override {
        for (int i = 0; i < side; i++) {
            for (int j = 0; j < side; j++) {
                cout << "* ";
            }
            cout << endl;
        }
    }
};

int main() {
    Square square(4);
    square.draw();

    return 0;
}
반응형