캡술화 /재사용의 단위 /main함수 위에 있는 것들은 재사용이 가능
#include <iostream>
using namespace std;
class Dog {
private:
int age;
double weight;
string name;
public:
int getAge() {
return age;}
void setAge(int a) {
age = a;}
double getWeight() {
return weight;}
void setWeight(double w) {
weight = w;}
string getName() {
return name;}
void setName(string n) {
name = n;}};
int main(){
Dog happy;
happy.setAge(3);
happy.setWeight(3.5);
happy.setName("해피");
cout << happy.getName()<<"는 "<<happy.getAge()<<"살, "<<happy.getWeight()<<"kg입니다.\n";
return 0;}

문자열 저장 std::string -;
함수의 문자열 리턴, std::string
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std; //srd::생략
class Cat {
private: //생략가능
int age;
string name; // C
public:
int getAge();
string getName();
void setAge(int a);
void setName(string pName);
};
int Cat::getAge()
{return age;}
void Cat::setAge(int a)
{age = a;}
void Cat::setName(string pName)
{// strcpy(name, pName);
name = pName; //C}
string Cat::getName()
{return name;}
int main(){
Cat nabi;
nabi.setName("나비");
nabi.setAge(3); //입력
cout << nabi.getName() << " 나이는"<<nabi.getAge()<<"살이다.";
return 0;}
#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(i);
cout << dd[i].getAge();} //01234
return 0;}
호출할 때가 중요
즉, 생성자는 주로 멤버변수의 초기화를 한다.
C,C++초기화 방법
생성자가 만들어지면 자동호출
방법3개가 있음, 2,3번쨰 방법을 더 자주 사용하는 것이 좋음
C++초기화방법 3가지(시험낸적 있음)
int x=1 비추인 이유 간단한 x=1은 문제없음, 매우 복잡한것들은 성능상의 문제로 비추
개체를 만들 떄 가로열고 초기값을 작성해야함 // initializerlist시험에 많이냄 // 위 둘 소스는 동일한 소스
#include <iostream>
using std::cout;
class Dog {
private:
int age;
public:
Dog(int age) {this-> age = age; }
~Dog() { cout << "소멸"; }
int getAge();
void setAge(int a);};
int Dog::getAge(){return age;}
void Dog::setAge(int age){this->age = age;} //멤버변수age=매개변수age
int main(){
Dog happy(5);
cout << happy.getAge();
return 0;}
this포인터는 ->로 접근
#include <iostream>
using std::cout;
class Dog {
private:
int age;
public:
Dog(int age);
~Dog();
int getAge();
void setAge(int a);
};
Dog::Dog(int age) { this->age = age; }
Dog::~Dog() { cout << "소멸"; }
int Dog::getAge()
{
return age;
}
void Dog::setAge(int age)
{
this->age = age;
} //멤버변수age=매개변수age
int main()
{
Dog happy(5);
cout << happy.getAge();
return 0;
}

#include <iostream>
using namespace std;
class Cat {
private:
int age;
double weight;
string name;
public:
Cat(int age, double weight, string name);
~Cat();
int getAge();
void setAge(int a);
double getWeight();
void setWeight(double w);
string getName();
void setName(string n);
void meow();
};
Cat::Cat(int age, double weight, string name)
{
this->age = age;
this->weight = weight;
this->name = name;
}
Cat::~Cat() { cout << "소멸"; }
int Cat::getAge()
{return age;}
void Cat::setAge(int age)
{this->age = age;} //멤버변수age=매개변수age
double Cat::getWeight()
{return weight;}
void Cat::setWeight(double w)
{this->weight = w;}
string Cat::getName()
{return name;}
void Cat::setName(string n)
{this->name = n;}
void Cat::meow()
{cout << "애옹.\n";}
int main()
{
Cat happy(5, 6.5, "나비");
cout << happy.getAge() << "\n";
cout << happy.getName() << "\n";
cout << happy.getWeight() << "\n";
happy.meow();
return 0;
}
주석 뤼튼
#include <iostream>
#include <string> // 문자열을 사용하기 위해 추가
using namespace std;
// Cat 클래스 정의
class Cat {
private:
int age; // 고양이의 나이를 저장할 private 변수
double weight; // 고양이의 몸무게를 저장할 private 변수
string name; // 고양이의 이름을 저장할 private 변수
public:
// 생성자: 나이, 몸무게, 이름을 매개변수로 받아 초기화
Cat(int age, double weight, string name);
// 소멸자: Cat 객체가 소멸될 때 호출됨
~Cat();
// 나이를 반환하는 메서드
int getAge();
// 나이를 설정하는 메서드
void setAge(int a);
// 몸무게를 반환하는 메서드
double getWeight();
// 몸무게를 설정하는 메서드
void setWeight(double w);
// 이름을 반환하는 메서드
string getName();
// 이름을 설정하는 메서드
void setName(string n);
// "애옹" 소리를 출력하는 메서드
void meow();
};
// 생성자 구현
Cat::Cat(int age, double weight, string name)
{
this->age = age; // 매개변수 age로 멤버변수 age 초기화
this->weight = weight; // 매개변수 weight로 멤버변수 weight 초기화
this->name = name; // 매개변수 name으로 멤버변수 name 초기화
}
// 소멸자 구현: 객체가 소멸될 때 호출됨
Cat::~Cat() { cout << "소멸\n"; } // 소멸 메시지 출력
// 나이를 반환하는 메서드 구현
int Cat::getAge() { return age; }
// 나이를 설정하는 메서드 구현
void Cat::setAge(int age) { this->age = age; } // 멤버변수 age = 매개변수 age
// 몸무게를 반환하는 메서드 구현
double Cat::getWeight() { return weight; }
// 몸무게를 설정하는 메서드 구현
void Cat::setWeight(double w) { this->weight = w; } // 멤버변수 weight = 매개변수 w
// 이름을 반환하는 메서드 구현
string Cat::getName() { return name; }
// 이름을 설정하는 메서드 구현
void Cat::setName(string n) { this->name = n; } // 멤버변수 name = 매개변수 n
// "애옹" 소리 출력 메서드 구현
void Cat::meow() { cout << "애옹.\n"; }
int main() {
Cat happy(5, 6.5, "나비"); // 나이 5, 몸무게 6.5kg, 이름 "나비"인 Cat 객체 생성
// 고양이의 나이, 이름, 몸무게 출력
cout << happy.getAge() << "\n"; // 나이 출력
cout << happy.getName() << "\n"; // 이름 출력
cout << happy.getWeight() << "kg\n"; // 몸무게 출력
happy.meow(); // "애옹." 출력
return 0; // 프로그램 종료
}
'인덕대 C++-출처 smile han' 카테고리의 다른 글
10주차 (6) | 2024.11.04 |
---|---|
10주차 예습 (0) | 2024.11.02 |
C++9주차 예습 (0) | 2024.10.27 |
C++ 7주차 (0) | 2024.10.14 |
C++ 7주차 예습 (1) | 2024.10.13 |