upload
This commit is contained in:
36
CplusplusPractice/class.cc
Normal file
36
CplusplusPractice/class.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
|
||||
class Animal
|
||||
{
|
||||
private:
|
||||
int food;
|
||||
int weight;
|
||||
|
||||
public:
|
||||
void setAnimal(int _food, int _weight)
|
||||
{
|
||||
food = _food;
|
||||
weight = _weight;
|
||||
}
|
||||
void increaseFood(int inc)
|
||||
{
|
||||
food += inc;
|
||||
weight += inc / 3;
|
||||
}
|
||||
void viewStat()
|
||||
{
|
||||
std::cout << "이 동물의 Food : " << food << std::endl;
|
||||
std::cout << "이 동물의 Weight : " << weight << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Animal animal;
|
||||
animal.setAnimal(100, 50);
|
||||
animal.increaseFood(30);
|
||||
|
||||
animal.viewStat();
|
||||
|
||||
return 0;
|
||||
}
|
||||
56
CplusplusPractice/classpractice.cc
Normal file
56
CplusplusPractice/classpractice.cc
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class Test
|
||||
{
|
||||
std::string str;
|
||||
|
||||
public:
|
||||
Test(std::string _str)
|
||||
{
|
||||
str = _str;
|
||||
std::cout << "생성자 호출! " << str << std::endl;
|
||||
}
|
||||
~Test()
|
||||
{
|
||||
std::cout << "소멸자 호출! " << str << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
class Foo
|
||||
{
|
||||
int number;
|
||||
|
||||
public:
|
||||
Foo(int inp) : number(inp) {}
|
||||
|
||||
int& accessNumber() { return number; }
|
||||
int getNumber() { return number; }
|
||||
void showNumber() { std::cout << number << std::endl; }
|
||||
};
|
||||
|
||||
void simpleFunction()
|
||||
{
|
||||
Test B("B Class");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Test A("A Class");
|
||||
// simpleFunction();
|
||||
|
||||
Foo a(5);
|
||||
a.showNumber();
|
||||
|
||||
int& c = a.accessNumber();
|
||||
c = 4;
|
||||
a.showNumber();
|
||||
|
||||
int d = a.accessNumber();
|
||||
d = 3;
|
||||
a.showNumber();
|
||||
|
||||
int f = a.getNumber();
|
||||
f = 1;
|
||||
a.showNumber();
|
||||
}
|
||||
31
CplusplusPractice/classtest.cc
Normal file
31
CplusplusPractice/classtest.cc
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
int x;
|
||||
|
||||
public:
|
||||
A(int c) : x(c) {}
|
||||
A(const A &a) {
|
||||
x = a.x;
|
||||
std::cout << "복사 생성" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
class B {
|
||||
A a;
|
||||
|
||||
public:
|
||||
B(int c) : a(c) {}
|
||||
B(const B &b) : a(b.a) {}
|
||||
A get_A() {
|
||||
A temp(a);
|
||||
return temp;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
B b(10);
|
||||
|
||||
std::cout << "---------" << std::endl;
|
||||
A a1 = b.get_A();
|
||||
}
|
||||
80
CplusplusPractice/dateclass.cc
Normal file
80
CplusplusPractice/dateclass.cc
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
|
||||
class Date
|
||||
{
|
||||
private:
|
||||
int year_, month_, day_;
|
||||
|
||||
int getMonthsMaxDay(int year, int month)
|
||||
{
|
||||
int months[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
if (month == 2)
|
||||
{
|
||||
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
||||
months[1] = 29;
|
||||
else months[1] = 28;
|
||||
}
|
||||
|
||||
return months[month - 1];
|
||||
}
|
||||
|
||||
public:
|
||||
Date();
|
||||
Date(int year, int month, int day);
|
||||
void addDay(int inc);
|
||||
void addMonth(int inc);
|
||||
void addYear(int inc);
|
||||
void showDate();
|
||||
};
|
||||
|
||||
Date::Date()
|
||||
: year_(2004), month_(01), day_(01) {}
|
||||
|
||||
Date::Date(int year, int month, int day)
|
||||
: year_(year), month_(month), day_(day) {}
|
||||
|
||||
void Date::addDay(int inc)
|
||||
{
|
||||
day_ += inc;
|
||||
|
||||
for (; day_ > getMonthsMaxDay(year_, month_);)
|
||||
{
|
||||
day_ -= getMonthsMaxDay(year_, month_);
|
||||
addMonth(1);
|
||||
}
|
||||
}
|
||||
|
||||
void Date::addMonth(int inc)
|
||||
{
|
||||
month_ += inc;
|
||||
|
||||
for (; month_ > 12;)
|
||||
{
|
||||
month_ -= 12;
|
||||
addYear(1);
|
||||
}
|
||||
}
|
||||
|
||||
void Date::addYear(int inc)
|
||||
{
|
||||
year_ += inc;
|
||||
}
|
||||
|
||||
void Date::showDate()
|
||||
{
|
||||
if (month_ < 10 && day_ < 10)
|
||||
std::cout << "Now Date() : " << year_ << "-0" << month_ << "-0" << day_ << std::endl;
|
||||
else if (month_ < 10 && day_ > 9)
|
||||
std::cout << "Now Date() : " << year_ << "-0" << month_ << "-" << day_ << std::endl;
|
||||
else if (month_ > 9 && day_ < 10)
|
||||
std::cout << "Now Date() : " << year_ << "-" << month_ << "-0" << day_ << std::endl;
|
||||
else
|
||||
std::cout << "Now Date() : " << year_ << "-" << month_ << "-" << day_ << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Date date(2020, 10, 10);
|
||||
date.showDate();
|
||||
}
|
||||
15
CplusplusPractice/header1.h
Normal file
15
CplusplusPractice/header1.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace header1
|
||||
{
|
||||
int foo()
|
||||
{
|
||||
std::cout << "This is H1 - foo()" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
void bar()
|
||||
{
|
||||
std::cout << "This is H1 - bar()" << std::endl;
|
||||
}
|
||||
}
|
||||
15
CplusplusPractice/header2.h
Normal file
15
CplusplusPractice/header2.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace header2
|
||||
{
|
||||
int foo()
|
||||
{
|
||||
std::cout << "This is H2 - foo()" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
void bar()
|
||||
{
|
||||
std::cout << "This is H2 - bar()" << std::endl;
|
||||
}
|
||||
}
|
||||
62
CplusplusPractice/legend.cc
Normal file
62
CplusplusPractice/legend.cc
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <cstdio>
|
||||
|
||||
class legend
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
legend();
|
||||
~legend();
|
||||
|
||||
void q1();
|
||||
void q2();
|
||||
};
|
||||
|
||||
legend::legend()
|
||||
{
|
||||
printf("default create function call\n");
|
||||
}
|
||||
|
||||
legend::~legend()
|
||||
{
|
||||
printf("default delete function call");
|
||||
}
|
||||
|
||||
// 1번 문제
|
||||
void legend::q1()
|
||||
{
|
||||
for (int i = 4; i >= 0; --i)
|
||||
{
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
for (int k = 0; k < 9 - (i * 2); ++k)
|
||||
{
|
||||
printf("*");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
printf(" ||| \n");
|
||||
}
|
||||
|
||||
// 2번 문제
|
||||
void legend::q2()
|
||||
{
|
||||
double scoreAvg;
|
||||
printf("이번 학기에 받고 싶은 전과목 평균 점수가 무엇인고?\n");
|
||||
scanf("%lf", &scoreAvg);
|
||||
|
||||
printf("너의 이번 학기 전과목 평균 점수는 %.1lf점이 될 것이니라~\n", scoreAvg);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
legend legeno;
|
||||
|
||||
legeno.q1();
|
||||
legeno.q2();
|
||||
}
|
||||
11
CplusplusPractice/namespace.cc
Normal file
11
CplusplusPractice/namespace.cc
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <iostream>
|
||||
#include "header1.h"
|
||||
#include "header2.h"
|
||||
|
||||
using namespace header1;
|
||||
|
||||
int main()
|
||||
{
|
||||
foo();
|
||||
header2::foo();
|
||||
}
|
||||
Reference in New Issue
Block a user