//변형 전
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using std::cout;
class Cat {
private: //생략가능
	int age;
	char name[20];
	// const char* name; //A
public:
	Cat(int age, const char* n) {
		this->age = age;
		strcpy(name, n); // name=n; //A
		cout << name << "고양이 객체가 만들어졌어요.\n";
	}
	~Cat() { cout << name << "객체 바이\n"; };
	int getAge();
	const char* getName();
	void setAge(int age);
	void setName(const char* pName);
	void meow();
};
int Cat::getAge() {
	return age;
}
void Cat::setAge(int age) {
	this->age = age;
}
void Cat::setName(const char* pName) {
	strcpy(name, pName);
	//strcpy(대상주소, 원본주소);
	//strcpy_s(대상주소, 대상의길이, 원본주소);
	//name=pName; //A
}
const char* Cat::getName() {
	return name;
}
void Cat::meow() {
	cout << name << "고양이가 울어요\n";
}
int main() {
	Cat nabi(1, "나비"), yaong(1, "야옹"), * pNabi;
	cout << nabi.getName() << " 출생 나이는 " << nabi.getAge() << "살이다.\n";
	cout << yaong.getName() << " 출생 나이는 " << yaong.getAge() << "살이다.\n";
	pNabi = &nabi;
	cout << pNabi->getName() << " 출생 나이는 " << pNabi->getAge() << "살이다.\n";
	nabi.setName("Nabi");
	nabi.setAge(3);
	cout << nabi.getName() << " 나이는 " << nabi.getAge() << "살이다.\n";
	yaong.meow();
	nabi.meow();
	return 0;
}
//변형 후(1)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using std::cout;
class Cat {
private: //생략가능
	int age;
	std::string name;
	
public:
	Cat(int age, std::string n) {
		this->age = age;
		name= n; 
		cout << name << "고양이 객체가 만들어졌어요.\n";
	}
	~Cat() { cout << name << "객체 바이\n"; };
	int getAge();
	std::string getName();
	void setAge(int age);
	void setName(std::string pName);
	void meow();
};
int Cat::getAge() {
	return age;
}
void Cat::setAge(int age) {
	this->age = age;
}
void Cat::setName(std::string pName) {
	name=pName;

}
std::string Cat::getName() {
	return name;
}
void Cat::meow() {
	cout << name << "고양이가 울어요\n";
}
int main() {
	Cat nabi(1, "나비"), yaong(1, "야옹"), * pNabi;
	cout << nabi.getName() << " 출생 나이는 " << nabi.getAge() << "살이다.\n";
	cout << yaong.getName() << " 출생 나이는 " << yaong.getAge() << "살이다.\n";
	pNabi = &nabi;
	cout << pNabi->getName() << " 출생 나이는 " << pNabi->getAge() << "살이다.\n";
	nabi.setName("Nabi");
	nabi.setAge(3);
	cout << nabi.getName() << " 나이는 " << nabi.getAge() << "살이다.\n";
	yaong.meow();
	nabi.meow();
	return 0;
}

#define IN 1 // 컴파일 전에 IN을 찾아서 1로 바꿈
#include <iostream>
int main()
{
	const int x = 2; // 변수 x는 항상 1, 변경 불가, 초기값 지정해야
	int const y = 3; // 비추, const는 자료형 앞에 씀
	const int z{ 4 }; // Uniform initialization, C++11, z{}
	constexpr int a = 5; //C++11부터 가능, compile-time constant
	//x = 2; //변경 불가
	//y=10;
	//z= 20;
	//const지정을 하면 변경 불가
	std::cout << IN << x << y << z << a;
	return 0;
}

-멤버 변수를 변경하지 않는 함수는 뒤에 const를 쓴다

//오류 수정 전
#include <iostream>
class Dog {
	int age; //private 생략함
public:
	int getAge() const;
	void setAge(int a) { age = a; }
	void view() { std::cout << "나는 view"; }
};
int Dog::getAge() const
{
	view(); // 오류 ①
	return (++age); // 오류 ②
}
int main()
{
	Dog happy;
	happy.setAge(5);
	std::cout << happy.getAge();
	return 0;
}

//수정 후
#include <iostream>
class Dog {
	int age; //private 생략함
public:
	int getAge() const;
	void setAge(int a) { age = a; }
	void view() const
	{ 
		std::cout << "나는 view";
	}
};
int Dog::getAge() const
{
	view(); // 오류 ① view를 const로 지정해줘야지 호출 가능함
	return age; // 오류 ② 멤버 변수 age를 변경해서 나는 오류
}
int main()
{
	Dog happy;
	happy.setAge(5);
	std::cout << happy.getAge();
	return 0;
}

   

const함수는 const함수만 호출할 수 있으며 일반 멤버함수에는 접근할 수 없다.

#include <iostream>
class Dog {
	int age; //private 생략함
public:
	int getAge() const;
	void setAge(int a) {
		age = a;
	}//const는 반드시 지정할 필요는 없는데 쓰면 소스를 이해하는데 수월하다

	
};
int Dog::getAge() const
{
	
	return age; // 오류 ② 멤버 변수 age를 변경해서 나는 오류
}
int main()
{
	Dog happy;
	happy.setAge(5);
	std::cout << happy.getAge();
	return 0;
}
#include <iostream>
class Dog {
	int age; //private 생략함
public:
	int getAge() const;
	void setAge(int a) {
		age = a;
	}//const는 반드시 지정할 필요는 없는데 쓰면 소스를 이해하는데 수월하다

	
};

int main()
{
	Dog happy;
	happy.setAge(5);
	std::cout << happy.getAge();
	return 0;
}
#include <iostream>
int main()
{
	int array[1000000]; //4MB, 지역변수는 스택에 저장하는데 기본 스택 크기를 넘어서 오류
	std::cout << "aaaa";
	return 0;
}//VS에서 실행될까?

//동적 메모리 할당, 동적 메모리 할당

#include <iostream>
int main()
{
	int *pi = new int; // 동적 메모리 할당, heep
	int x; //정적 메모리 할당, 스택
	if (!pi) { // pi==0, 널 포인터인지 확인
		std::cout << "메모리할당이 되지 않았습니다.";
		return 1; //비정상 종료시 리턴값
	}
	*pi = 100; //주소의 값으로 100을 할당
	x = 10;
	std::cout << "동적메모리=" << *pi << ", x=" << x;
	delete pi; // 메모리 해제
	return 0; // 정상 종료시 리턴값
}

-대괄호 쓰는거 주의하기

-new를 사용하면 반드시 delete를 써야 한다 , 안쓰면 다른 프로그램에서 사용 불가

-배열의 이름은 배열의 시작 주소이다

ex} int x[3] ={1,2,3} 일때 x는 1이 저장된 곳의 주소

           x[0];//1

           x[1];//2

           x[2];//3

//실습과제
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using std::cout;
class Cat {
private: //생략가능
	int age;
	std::string name;
public:
	Cat(int age, std::string n) {
		this->age = age;//int age;
		name = n;
		cout << name << "고양이 객체가 만들어졌어요.\n";
	}
	~Cat() { cout << name << "객체 바이\n"; }; //소멸자
	int getAge() const; //변경 안됨
	std::string getName()const;
	void setAge(int age);
	void setName(std::string pName);
	void meow()const;
};
int Cat::getAge() const {
	return age;
}
void Cat::setAge(int age) {
	this->age = age;
}
void Cat::setName(std::string pName) {
	name = pName;
}
std::string Cat::getName() const {
	return name;
}
void Cat::meow() const {
	cout << name << "고양이가 울어요\n";
}
int main() {
	Cat nabi(1, "나비"), yaong(1, "야옹"), * pNabi;
	cout << nabi.getName() << " 출생 나이는 " << nabi.getAge() << "살이다.\n";
	cout << yaong.getName() << " 출생 나이는 " << yaong.getAge() << "살이다.\n";
	pNabi = &nabi;
	cout << pNabi->getName() << " 출생 나이는 " << pNabi->getAge() << "살이다.\n";
	nabi.setName("Nabi");
	nabi.setAge(3);
	cout << nabi.getName() << " 나이는 " << nabi.getAge() << "살이다.\n";
	yaong.meow();
	nabi.meow();
	return 0;
}
#include <iostream>
class Dog {
private:
	int age;
public:
	int getAge();
	void setAge(int a);
};
int Dog::getAge()
{
	return age;
}
void Dog::setAge(int a)
{
	age = a;
}
int main()
{
	Dog* dp;//배열의 이름은 배열의 시작 주소이다
	dp = new Dog[10]; // 객체배열 할당 배열을 동적으로 할당했을떄는 대괄호를 써야함
	// Dog *dp=new Dog[10];
	if (!dp) {
		std::cout << "메모리할당이 되지 않았습니다.";
		return 1;
	}
	for (int i = 0; i < 10; i++) // C++에서는 가능
		dp[i].setAge(i);
	for (int i = 0; i < 10; i++)
		std::cout << i << "번째 객체의 나이는 " <<
		dp[i].getAge() << " 입니다. " << std::endl;
	delete[]dp;
	return 0;
}

 출처: 1학년 2학기 수업자료 / 한성현 교수님

'C++프로그래밍' 카테고리의 다른 글

C++프로그래밍 12주차 과제  (1) 2023.11.22
C++프로그래밍 11주차 과제  (0) 2023.11.15
C++ 프로그래밍 9주차 과제  (1) 2023.11.01
C++ 프로그래밍 6주차 과제  (0) 2023.10.18
C++프로그래밍 5주차 과제  (1) 2023.10.11

+ Recent posts