본 게시글은 필기 형식으로 작성한 것이며 주석처리로 공부 내용을 적어두었습니다.
정리가 되지 않은 상태로 순서가 뒤죽박죽일 수 있습니다.
// 객체지향 프로그래밍 필요한 이유
//객체라는 것은
//Friend : name, address, age, height, weight, .... 이런 변수들과
// print()같이 function들을 묶어 놓은 것을 이야기 함
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//친구들의 명단을 정리한다면
// Firend : name, address, age, height, weight, ...
class Friend //object라는 것을 문법적으로 표현하는 것
{
public: //access specifier (public, private, protected)
string name;
string address;
int age;
double height;
double weight;
//print라는 function도 member가 됨. 즉 바로 접근이 가능하다.
void print()
{
cout << name << " " << address << " " << age << " " << height << " " << weight << endl;
}
};
void print(const Friend &fr)
{
cout << fr.name << " " << fr.address << " " << fr.age << " " << fr.height << " " << fr.weight << endl;
//fr이 중복되는 느낌 -> 이 함수를 struct 안에 넣어버림
}
void print(const string &name, const string &address, const int &age, const double &height, const double &weight)
{
cout << name << " " << address << " " << age << " " << height << " " << weight << endl;
}
int main()
{
// 여기에 여러개의 변수를 정의하는 것 보다 struct로 하나로 묶음
//instanciation. 실제 메모리에 차지하게 만들어 주는 것
//아래의 것은 class의 instance
Friend jj{"Jack Jack", "Uptown", 2, 30, 10};
jj.print();
//밑에 코드처럼 하나하나 vector를 만드는 것 보다 Friend의 vector를 만들면 된다.
vector<Friend> my_friends;
my_friends.resize(2);
//몇번째 친구의 정보를 출력
for (auto &ele : my_friends)
{
ele.print();
}
//print(jj);
//print(jj.name, jj.address, jj.age, jj.height, jj.weight) ;
//매개변수를 위처럼 하는게 아니라 하나의 struct로 받아버리는 것
// vector<string> name_vec;
// vector<string> addr_vec;
// vector<int> age_vec;
// vector<double> height_vec;
// vector<double> weight_vec;
// //특정한 index값을 넣어주어야 함, 매번 넣어주는 것이 꽤나 불편함
// print(name_vec[0], addr_vec[0], age_vec[0], height_vec[0], weight_vec[0]);
// ------------------
//struct를 만듬
return 0;
}
-----------------
Encapsulation (캡슐화), Access Specifiers (접근 지정자) Access functions (접근 함수)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Date
{
//public: //access specifier로 클래스 안의 변수들을 밖에서 접근하게 or not
int m_month;
int m_day;
int m_year;
public:
void setDate(const int &month_input, const int &day_input, const int &year_input)
{
m_month = month_input;
m_day = day_input;
m_year = year_input;
}
//variable들은 private이더라도 function은 Encapsulation을 통해서 접근 가능하게 할 수 있음
void setMonth(const int &month_input)
{
m_month = month_input;
}
void setDay(const int &day_input)
{
m_day = day_input;
}
void setYear(const int &year_input)
{
m_year = year_input;
}
//위는 setters 아래는 getters
//VO 설정할때 사용하던 것
//프린트 할때는 return 값으로 직접 보내주는 방법
const int &getDay()
{
return m_day;
}
const int &getMonth()
{
return m_month;
}
const int &getYear()
{
return m_year;
}
void copyFrom(const Date &original)
{
m_month = original.m_month;
m_day = original.m_day;
m_year = original.m_year;
}
};
int main()
{
Date today;
// today.m_month = 3;
// today.m_day = 4;
// today.m_year = 2021
today.setDate(3, 1, 2021);
today.setMonth(4);
//cout << today.getday() << endl;
Date copy;
copy.copyFrom(today);
return 0;
}
--------------------
생성자 COnstructors
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//분수를 나타내는 클래스
class Fraction
{
public:
int m_numerator;
int m_denominator;
public:
// 생성자라는 것을 만듬
//반환하는 것이 없고 클래스와 이름이 동일 하면 생성자
//외부에서 쓰려는 것이 아니고 선언과 동시에 생성이 된다
Fraction(const int &num_int, const int &den_int = 1)
{
m_numerator = num_int;
m_denominator = den_int;
cout << "Fraction() constructor" << endl;
}
void print()
{
cout << m_numerator << "/" << m_denominator << endl;
}
};
int main()
{
//Fraction frac; //생성자의 parameter가 없을 경우에 빼야함!
// frac.m_numerator = 0;
// frac.m_denominator = 1;
//frac.print();
Fraction one_thirds(1, 3);
one_thirds.print();
Fraction one_one(1);
one_one.print();
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Second
{
public:
Second()
{
cout << "class Second constructor()" << endl; //debug하는 방법으로 프린트 찍어줌
}
};
class First
{
Second sec;
public:
First()
{
cout << "class First constructor()" << endl;
}
};
int main()
{
//Second 생성자가 먼저 실행되고 First가 실행된다.
First fir;
return 0;
}
위와 같은 코드에서는 Second가 먼저 생성되고 그 뒤에 First가 생성되는 것을 알 수 있다.
-------------
Constructor's Member initializer List