3일차 정리
★★insert키를 누르면 지워지는게 사라진다.
@---구조체
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct jumsu{
int kor, eng, math;
}JUMSU, * PJUMSU;
void main ( ) {
int a, b, c;
cin >> a;
cin >> b;
cin >> c;
JUMSU st={a,b ,c };
cout<<st.kor<< st.eng << st.math<< endl;
cout << "점수의 총점"<< st.kor + st.eng + st.math <<endl;
}
-----실행결과
1 2 3
123
점수의 총점6
계속하려면 아무 키나 누르십시오 . . .
-------------
typedef(자료형) struct(구조체) 구조체명 {
}JUMSU(자료명), *PJUMSU(포인트명)
-------예제, 구조체 안에 함수 포함
#include "stdafx.h"
#include <iostream>
using namespace std;
struct jumsu {
int kor, eng, math;
int sum(int kor, int eng,int math){
return kor + eng + math;
}
};
void main () {
jumsu st= {95, 80, 20};
cout << "총점 = " << st.sum(st.kor, st.eng, st.math) << endl;
}
----------실행결과
총점 = 195
계속하려면 아무 키나 누르십시오 . . .
------------------
클래스
using namespace std;
class jumsu {
public:
int kor, eng, math;
int sum(int kor, int eng,int math){
return kor + eng + math;
}
};
void main () {
jumsu st= {95, 80, 20};
cout << "총점 = " << st.sum(st.kor, st.eng, st.math) << endl;
}
===========구조체를 변경한 클래스
using namespace std;
class jumsu {
private:
int kor, eng, math;
public:
void SetJumsu(int a, int b, int c) { kor = a; eng = b; math = c;}
int sum() { return kor + eng + math;}
};
void main () {
jumsu st;
st.SetJumsu(95, 80, 20);
cout << "총점 = " << st.sum() << endl;
}
--------------출력부분도 구조체 안으로
#include "stdafx.h"
#include <iostream>
using namespace std;
class jumsu {
private:
int kor, eng, math; //은닉화
public:
void SetJumsu(int a, int b, int c) { kor = a; eng = b; math = c;}
int sum() { return kor + eng + math;}
void display(){cout << "총점 = " << sum() << endl;}
};
void main () {
jumsu st;
st.SetJumsu(95, 80, 20);
st.display();
}
-------------------실행결과 일치
원기둥의 부피를 구하는 class를 생성하시오
using namespace std;
class cylinder {
private:
int radius, height;
public:
void Setvlaue(int num1, int num2) {
radius = num1; height=num2;
}
double volume() {
return radius * radius * height * 3.14;
}
void Display() { cout << "반지름 : "<< volume() << endl; }
};
void main () {
cylinder cy;
cy.Setvlaue(1, 2);
cy.Display();
}
---------결과
반지름 : 6.28
계속하려면 아무 키나 누르십시오 . . .
-------사각형의 넓이와 둘레를 구하는 class를 생성하시오
using namespace std;
class quadrangle {
private:
int width, legnth;
public:
void Setvlaue(int num1, int num2) {
width = num1; legnth=num2;
}
int extent() {
return width * legnth;
}
int circumference() {
return width * 2 + legnth * 2;
}
void Display() {
cout << "넓이 : "<< extent() << endl;
cout << "둘레 : "<< circumference() << endl;
}
};
void main () {
quadrangle qr;
int wid, leng;
cin >> wid;
cin >> leng;
qr.Setvlaue (wid,leng);
qr.Display();
}
------결과
1
2
넓이 : 2
둘레 : 6
계속하려면 아무 키나 누르십시오 . . .
-----육면체의 부피를 구하는 class를 생성하시오
//육면체의 부피를 구하는 class를 생성하시오
#include "stdafx.h"
#include <iostream>
using namespace std;
class hexahedron {
private:
int width, height, length;
public:
void setvalue(int a,int b,int c){
width=a;
height=b;
length=c;
}
int GetVolume() {
return width * height * length;
}
};
void main () {
hexahedron hd;
int a,b,c;
cin >> a;
cin >> b;
cin >> c;
hd.setvalue(a,b,c);
cout << "결과값은 바로 " << hd.GetVolume() << endl;
}
--------결과
2
3
4
결과값은 바로 24
계속하려면 아무 키나 누르십시오 . . .
@생성자-------------
실행시 생성자가 실행됨
처음에만 자동호출이고 두번째는 값을 초기화 시키기 때문에 안된다ㅣ.
생성자는 임의로 호출이 안되는 함수이다.
--------
using namespace std;
class Point {
int xp, yp;//private가 없더라도
public전까지는 private
public:
Point(){ //생성자는 클래스와 이름이 같다.
xp=yp=5;
}
int GetX() { return xp;}
int GetY() { return yp;}
};
void main () {
Point p1; //클래스 객체 생성;시 생성자 실행.
cout <<"p1 -> x: " <<p1.GetX() <<", y:" <<p1.GetY()<<endl;
}
----------------실행결과
p1 -> x: 5, y:5
계속하려면 아무 키나 누르십시오 . . .
-----------------
@-----생성자를 변수처럼 활용
class Point {
int xp, yp;
public:
Point(int x, int y){ //생성자
xp=x; yp=y;
}
int GetX() { return xp;}
int GetY() { return yp;}
};
void main () {
Point p1(10,20);//point클래스(자료형)을 이용해 p1이란 객체를 마는
cout <<"p1 -> x: " <<p1.GetX() <<", y:" <<p1.GetY()<<endl;
}
-----실행결과
p1 -> x: 10, y:20
계속하려면 아무 키나 누르십시오 . . .
---------@객체 배열(생성자의 배열식)
using namespace std;
class Point {
int xp, yp;
public:
Point(int x, int y){ //생성자
xp=x; yp=y;
}
int GetX() { return xp;}
int GetY() { return yp;}
};
void main () {
Point p1[3]={Point(3,5), Point(20,40), Point(100,200)};
cout <<"p1[0].x : " <<p1[0].GetX() <<", y:" <<p1[0].GetY()<<endl;
cout <<"p1[1].x : " <<p1[1].GetX() <<", y:" <<p1[1].GetY()<<endl;
cout <<"p1[2].x : " <<p1[2].GetX() <<", y:" <<p1[2].GetY()<<endl;
}
--------실행결과
p1[0].x : 3, y:5
p1[1].x : 20, y:40
p1[2].x : 100, y:200
계속하려면 아무 키나 누르십시오 . . .
@--------------예제
#include "stdafx.h"
#include <iostream>
using namespace std;
class Sum{
int sum;
public:
Sum(){
int i;
sum =0;
for(i=1;i<=10;i++){
sum = sum + i ;
}
}
Sum(int num){
int i;
sum =0;
for(i=1;i<=num;i++){
sum = sum + i ;
}
}
Sum(int n1, int n2){
int max , min;
if( n1 > n2){
max = n1;
min = n2;
}
else {
max = n2;
min = n1;
}
int i;
sum = 0;
for(i=min;i<=max;i++){
sum += i;
}
}
int GetSum(){
return sum;
}
};
void main(){
Sum su;
cout << su.GetSum() << endl;
Sum su2(5);
cout << su2.GetSum() << endl;
Sum su3(1, 10);
cout << su3.GetSum() << endl;
}
---------------소멸자
#include "stdafx.h"
#include <iostream>
using namespace std;
class Con{
public:
Con(){ //처음 실행시 나타남
cout<<"constructor!!" << endl;
}
~Con() {//main함수 종료시 나타남
cout<<"Destructor!!" <<endl;
}
};
void main(){
Con c;
}
---------------실행결과
constructor!!
Destructor!!
계속하려면 아무 키나 누르십시오 . . .