IT공부/C++

1/10 명품 c++프로그래밍 03 open challenge, 연습이론문제

공부하는 라라 2024. 1. 9. 17:45

Open Challenge 143p

Exp.cpp

#include <iostream>
using namespace std;

#include "Exp.h"

int Exp::getBase()
{
	return base;
}

int Exp::getExp()
{
	return exp;
}
int Exp::getValue()
{
	//지수승 표현하기
	int num = exp;
		if (num == 1 && base ==1)
		{
			res = 1 ;
			return res;
		}
		else if (num == 1 && base != 1)
		{
			res = base;
			return res;
		}
		while (1)
		{
			if (num != 0)
			{
				res *= base;
				num--;
			}
			else
				return res;
		}
}

bool Exp::equals(Exp b)
{
	if (res == b.res)
		return true;
	else
		return false;
}

Exp.h

#ifndef ADDER_H
#define ADDER_H
class Exp
{
	int base;
	int exp;
public:
	int res;
	Exp()
	{
		base = 1; exp = 1; res = 1;
	};
	Exp(int x, int y)
	{
		base = x; exp = y; res = 1;
	}
	Exp(int z)
	{
		base = z; exp = 1; res = 1;
	}
	int getBase();
	int getExp();
	int getValue();
	bool equals(Exp b);
};
#endif

 

내 exp.cpp코드는 별로 안좋은 코드인것 같다. 

- res변수 하나를 public으로 옮긴것

- getValue도 간결하지 못하다고 생각한다. 

https://godog.tistory.com/entry/%EB%AA%85%ED%92%88-C-programming-3%EC%9E%A5-Open-Challenge 

 

명품 C++ programming 3장 Open Challenge

1. 문제 실수의 지수 표현을 클래스 Exp로 작성하라. Exp를 이용하는 main() 함수와 실행 결과는 다음과 같다. 클래스 Exp를 Exp.h 헤더 파일과 Exp.cpp 파일로 분리하여 작성하라. 2. 결과 3. 코드 // main 파

godog.tistory.com

이 분의 코드가 내 코드보다 더 간결하고 정제된것 같아서 이 분 코드로 고쳤다. ㅠㅠ

 

연습 이론문제

1. 객체의 내부를 볼수없게하고 외부로 부터 접근으로부터 보호하기 위해서. 

2. 3

3. private로 보호받고있는 멤버변수,함수가 없기 때문에 캡슐화가 아니다.

4.

class Circle
{
	int age;
	int radius;
public:
	double getArea();
	void older();
};

5. 맨 마지막 중괄호 } 뒤에 ; 를 붙인다

6. 생성자는 리턴타입을 선언해서는 안된다.

class Tower
{
	int height = 20;
public:
	Tower()
	{
		height = 10;
	}
};

7.

class Building
{
private:
	int floor;
public:
	Building(int s) { floor = s; }
	Building() { floor = 5; }
};

int main()
{
	Building twin, star;
	Building BlueHouse(5), JangMi(14);
}

8.

class Calendar
{
private:
	int year;
public:
	Calendar();
	int getYear();
};

Calendar::Calendar()
{
	year = 10;
}

int Calendar::getYear()
{
	return year;
}

9. 2

10. 3

11. 

(1)(2)

#include <iostream>
using namespace std;

class House
{
	int numOfRooms;
	int size;
public:
	House(int n, int s);
	~House();
};

House::House(int n, int s)
{
	numOfRooms = n; size = s;
	cout << "방 번호: " << numOfRooms << "그리고 크기 : " << size << endl;
}
House::~House()
{
	cout << "방 번호: " << numOfRooms << "그리고 크기 : " << size<<endl;
}

void f()
{
	House a(2, 20);
}
House b(3, 30), c(4, 40);

int main()
{
	f();
	House d(5, 50);
}

(3). 

b-c-a-a-d-d-c-b

12. c-b-a-a-b-c

13.  매개변수 없는 생성자 TV()가 private 지정자로 되어있어 main함수에서 생성자가 호출될수가 없다. 

class TV
{
public:
	int channels;
	TV(int a) { channels = a; }
	TV() { channels = 256; }
};

int main()
{
	TV LG;
	LG.channels = 200;
	TV Samsung(100);
}

14. channels 변수가 private로 지정되어있어 main함수에서 직접 접근이 불가능하다. 

class TV 
{
public:
	int colors;
	int channels;
	TV() { channels = 256; }
	TV(int a, int b) { channels = a; colors = b; }
};
int main()
{
	TV LG;
	LG.channels = 200;
	LG.colors = 60000;
	TV Samsung(100, 50000);
}

 

15. TV()생성자, TV(int a) 생성자, getChannels()함수

16. 2

17. 1

18. 1

19. 4

20.

class Family
{
private: 
	char tel[11];
public:
	int count;
	char address[20];
	Family();
};

21.

struct Universe
{
	Universe();
private:
	char dataCreated[10];
	char creator[10];
	int size;
};