💻 개인공부 💻/C | C++

[C++] 객체 지향을 이용한 c++ 프로그래밍

공대생 배기웅 2020. 4. 10. 17:15
반응형

<문제>

문제에서의 main()
요구사항

<요구 사항 분석>

1. Rectagle이라는 클래스를 지정해야 함

2. Rectangle클래스 안에 width height라는 정수형 지역 변수 설정 필요

3. 클래스 안에 getArea()라는 메소드를 설정 필요

4. getArea() width*height 값을 return하도록 코드 설계 필요

 

<설계도>

 

 <소스 코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
 
using namespace std;//std를 계속 쓰는 것을 막기 위해 사용
 
 
 
class Rectangle {//Rectangle이라는 클래스 선언
 
public://접근 권한을 public으로 설정
 
           int width;//width라는 지역 변수 선언
 
           int height;//height라는 지역 변수 선언
 
public:;//접근 권한을 public으로 설정
 
           int getArea() {//getArea라는 매소드를 Rectangle 클래스 안에 선언
 
                       return width * height;//return할 값
 
           }
 
           };
 
 
 
           int main() {//main함수 선언
 
                       Rectangle rect;//Rectangle클래스의 한 종류인 rect(생성자)를 선언
 
                       rect.width = 3;//rect 객체의 width 멤버값은 3
 
                       rect.height = 5;//rect 객체의 height 멤버값은 5
 
                       cout << "사각형의 면적은 :" << rect.getArea() << endl;//Rectangle 클래스의 종류인 rect의 getAreat값 출력
 
                       return 0;
 
           }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<의견>

처음에는 잘 못 느꼈지만 class를 이용해서 코딩을 하니 객체지향프로그래밍의 편리함을 느낄 수 있었다.

728x90
반응형