C++ 프로그래밍
Class 복습
#include<iostream> #include<string.h> using namespace std; class MyClass { private: int age; char id; char name[10]; public: void get(); void set(int a, char b, const char* c); }; void MyClass::get() { cout << "나이:" << age << endl << "id:" << id<< endl << "이름:" << name << endl; } void MyClass::set(int a, char b, const char* c) { age = a; id = b; strcpy(name, c); } int main() { MyClass o; o.set(10, 'C', "oyb"); o.get(); return 0; }
1) set 함수를 다음과 같이 구현하면 오류 발생 set(int a,char b,const charc);
2) c를 const char로 선언해줘서 해결
--> set(int a, char b, const char c) {age=a; id=b; strcpy(name,c);}
Class(생성자)
#include<iostream> #include<string.h> using namespace std; class myclass { private: int age; char id; char name[10]; public: myclass(); myclass(char aid, int aage, const char* aname); void get(); void set(int a, char b, const char* c); }; myclass::myclass() { age = 999; id = 'A'; strcpy(name, "생성자"); } void myclass::get() { cout << "나이:" << age << endl << "id:" << id<< endl << "이름:" << name << endl; } void myclass::set(int a, char b, const char* c) { age = a; id = b; strcpy(name, c); } int main() { myclass o; cout << "생성자에 의한 초기화" << endl; o.get(); cout << endl; o.set(10, 'c', "oyb"); cout << "set 함수에 의한 초기화" << endl; o.get(); return 0; }
Class(Calc)
#include<iostream> using namespace std; class Mycalc { private: int num1; int num2; public: Mycalc(); Mycalc(int n1, int n2); int add(); int sub(); int mul(); double div(); }; Mycalc::Mycalc(int n1, int n2) :num1(n1), num2(n2) { cout << "매개변수 있는 생성자 호출" << endl << n1 << "," << n2 << endl; } Mycalc::Mycalc() { cout << "생성자 호출" << endl; } int Mycalc::add() { return num1 + num2; } int Mycalc::sub() { return num1 - num2; } int Mycalc::mul() { return num1 * num2; } double Mycalc::div() { return double(num1) / double(num2); } int main() { Mycalc c; Mycalc d(2, 3); cout << d.add() << endl; cout << d.sub() << endl; cout << d.mul() << endl; cout << d.div() << endl; return 0; }
프로그래머스
배열뒤집기
def solution(num_list): answer = num_list answer.reverse() # answer=num_list.reverse() 처럼은 못씀 # num_list.reverse()하면 그냥 num_list 배열이 뒤집히는것 return answer solution([1,2,3,4,5])
1) answer=num_list.reverse()처럼 쓰려고함
2) num_list.reverse()하면 그냥 num_list배열이 뒤집히는거지 대입 불가능