#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	Dog(int a) { age = a; } // 생성자 정의, Dog():age(1){ }, Dog():age{1}{ }
	int getAge() { return age; }
	void setAge(int a) { age = a; }
};
int main()
{
	Dog happy(1), h(2); //happy객체가 생성되는 순간 생성자가 자동 호출됨
	cout << happy.getAge()<<h.getAge();
	return 0;
}

#include <iostream>
class Dog {
private:
	int age;
public:
	int getAge() {
		return age;
	}
	void setAge(int a) {//return값이 없으면 void를 쓴다
		age = a;
	}
};
int main()
{
	Dog happy;
	happy.setAge(3);
	// happy.age = 3;
	std::cout << happy.getAge();
	return 0;
}

직접참조연산자 : . 

-일반 객체가 멤버(변수/함수)에 접근하기 위해 사용

 간접참조연산자 : ->

- 포인터 객체가 멤버(변수/함수)에 접근하기 위해 사용

//c++07 ppt 6p오류 수정 후

#include <iostream>
using std::cout;
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 happy; // Dog class의 happy객체 정의

	happy.setAge(3); //  happy.setAge(3);
	cout << happy.getAge(); //cout<<happy.getAge()<<'\n';
	return 0;
}

 

 

//멍멍~~ 추가
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	int getAge();
	void setAge(int a);
	void cry() {
		cout << "멍멍~~\n";
	}
};
int Dog::getAge()
{
	return age;
}
void Dog::setAge(int a)
{
	age = a;
}
int main()
{
	Dog happy; // Dog class의 happy객체 정의

	happy.setAge(3); //  happy.setAge(3);
	cout << happy.getAge(); //cout<<happy.getAge()<<'\n';
	happy.cry();
	return 0;
}

 

#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	int getAge();
	void setAge(int a);
	void cry();
};
void Dog:: cry() {
	cout << "멍멍~~\n";
}
int Dog::getAge()
{
	return age;
}
void Dog::setAge(int a)
{
	age = a;
}
int main()
{
	Dog happy; // Dog class의 happy객체 정의

	happy.setAge(3); //  happy.setAge(3);
	cout << happy.getAge(); //cout<<happy.getAge()<<'\n';
	happy.cry();
	return 0;
}

 

//7p //using namespace std;주석 처리 했을때, string, cout 앞에 std::붙이기
#include <iostream>
//using namespace std;
class Dog {
private:
	int age;
	double weight;
	std::string name;
public:
	int getAge() {
		return age;
	}
	void setAge(int a) {
		age = a;
	}
	double getWeight() {
		return weight;
	}
	void setWeight(double w) {
		weight = w;
	}
	std::string getName() {
		return name;
	}
	void setName(std::string n) {
		name = n;
	}
};
int main()
{
	Dog happy;
	happy.setAge(3);
	happy.setWeight(3.5);
	happy.setName("해피");
	std::cout << happy.getName() << "는 "
		<< happy.getAge() << "살, "
		<< happy.getWeight() << "kg입니다.\n";
	return 0;
}

문자열을 복사할떄는 strcpy형태를 사용한다

 

//배열 복사는 strcpy() 사용
#define _CRT_SECURE_NO_WARNINGS //Visual Studio의 경우
#include <iostream>
#include <string> //or string.h(clang++, gcc 등 주로 온라인 컴파일러)
int main(void)
{
	char s1[5];
	char s2[5] = "soft"; //원본
	//s1 = s2; //error C3863: 배열 형식 'char [5]'은(는) 할당할 수 없습니다.
	strcpy(s1, s2); //s2주소의 문자열을 널 문자를 만날 때까지s1주소로 복사
	std::cout << "s1=" << s1 << " s2=" << s2 << std::endl;
	return 0;
}

 배열을 복사할 떄는 strcpy로 string형을 쓸때는 그냥 대입하면 된다.

//문자 or 문자열 리턴 자판기 함수 35p
#include <iostream>
//using namespace std;
char vending(int x)
{
	if (x == 1) return 'A';
	else return 'B';
}
std::string vending1(int x)
{
	if (x == 1) return "커피";
	else return "우유";
}
int main()
{
	std::cout << vending(1);
	std::cout << vending1(1);
	return 0;
}

int자리에 const char *를 써주면 문자열을 리턴하는게 가능하다

p38
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std; //C
class Cat {
private: //생략가능
	int age;
	std::string name; // A
public:
	int getAge();
	std::string getName();
	void setAge(int a);
	void setName(std::string pName);
};
int Cat::getAge()
{
	return age;
}
void Cat::setAge(int a)
{
	age = a;
}
void Cat::setName(std::string pName)
{
	// strcpy(name, pName);//A
	name = pName; //B
}
std::string Cat::getName()
{
	return name;
}
int main()
{
	Cat nabi;
	nabi.setName("나비");
	nabi.setAge(3); //입력
	cout << nabi.getName() << " 나이는"<<nabi.getAge()<<"살이다.";
		return 0;
}
//p39
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	int getAge() { return age; } //자동 inline함수
	void setAge(int a) { age = a; } //자동 inline함수
};
int main()
{
	int i;
	Dog dd[5]; //Dog클래스형 객체배열 dd, 강아지 5마리
	for (i = 0; i < 5; i++) {
		dd[i].setAge(1);
		cout << dd[i].getAge(); //01234
	}
	return 0;
}
//p40
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	int getAge() { return age; }
	void setAge(int a) { age = a; }
};
int main()
{
	Dog happy, * pd; //일반 객체 happy와 포인터 객체 pd, int x, *px;
	pd = &happy; //px=&x;
	happy.setAge(5); //일반 객체는 '.'으로 멤버를 접근 (중요)
	cout << happy.getAge() << pd->getAge(); //포인터 객체는 '->'로 멤버를 접근(중요)
	pd->setAge(2);
	cout << happy.getAge() << pd->getAge();
	return 0;
}

 

 

 

p48
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	//Dog(){age = 1;} //아래 두개 포함해서 3개는 같은 소스이다
	//Dog() : age (1) {};
	Dog() : age { 1 } {};
//	Dog() { age = 1; } // 생성자 정의, Dog():age(1){ }, Dog():age{1}{ }
	int getAge() { return age; }
	void setAge(int a) { age = a; }
};
int main()
{
	Dog happy, happy1; //happy객체가 생성되는 순간 생성자가 자동 호출됨
	cout << happy.getAge();
	cout << happy1.getAge();
	return 0;
}
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	Dog(){age = 1;}
	int getAge() { return age; }
	void setAge(int a) { age = a; }
};
int main()
{
	Dog happy, happy1; //happy객체가 생성되는 순간 생성자가 자동 호출됨
	cout << happy.getAge();
	cout << happy1.getAge();
	return 0;
}
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	Dog();
	int getAge();
	void setAge(int a);
};
Dog::Dog() {
	age = 1; 
}
int Dog::getAge() { return age; }
void Dog::setAge(int a) { age = a; }
int main()
{
	Dog happy; //happy객체가 생성되는 순간 생성자가 자동 호출됨
	cout << happy.getAge();
	return 0;
}
C++에서 변수를 초기화하는 방법
#include <iostream>
int main()
{
	int x = 1; //copy initialization,비추
	int y(2);//direct initialization, 괄호 안에 초기값 지정
	int z{ 3 };//Uniform initialization, C++11, 중괄호 안에 초기값 지정
	int z1{};//Uniform initialization, 자동으로 0,C++11
	std::cout << x << y << z << z1;
}
#include <iostream>
using std::cout;
class Dog {
private:
	int age;
public:
	Dog(int age) { 
		this -> age = age; 
	} 
	~Dog() { cout << "소멸\n"; }
	int getAge() { return age; }
	void setAge(int age) {
		this->age = age; //현재 클래스에 멤버 변수인 age를 가르킬 때는 앞에 this->를 쓴다.
	}
};
int main()
{
	Dog happy(1), h(2); //happy객체가 생성되는 순간 생성자가 자동 호출됨
	cout << happy.getAge()<<h.getAge();
	return 0;
}

 

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

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

C++프로그래밍 11주차 과제  (0) 2023.11.15
c++프로그래밍 10주차 과제  (1) 2023.11.08
C++ 프로그래밍 6주차 과제  (0) 2023.10.18
C++프로그래밍 5주차 과제  (1) 2023.10.11
C++ 프로그래밍 4주차 과제  (0) 2023.09.27

+ Recent posts