C언어 ver1 ,2 ,3
#include<iostream>
#include<string>
using namespace std;
class Person{
private:
string name;
static int cnt;
public:
Person(string n){name=n; cnt++;}
static int getCnt(){return cnt;}
void setName(string n){name=n;}
string getName(){return name;}
};
int Person::cnt=0;
int main()
{
Person p1("Kim");
Person p2("Kang");
Person p3("Lee");
cout<<"총인원:"<<Person::getCnt()<<endl;
}
Ver2
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
static int cnt;
public:
Person(string n){ name=n; cnt++;}
static int getCnt(){ return cnt;}
void setName(string n){ name=n; }
string getName(){return name; }
};
int Person::cnt=0;
class Student : public Person
{
private:
int quiz1, quiz2, sum;
float avg;
public:
Student(string n, int q1, int q2):Person(n){ quiz1=q1; quiz2=q2; }
void setQuiz(int q1, int q2){ quiz1=q1; quiz2=q2; }
void calcSumAvg(){sum=quiz1+quiz2; avg= sum/2.0;}
int getSum(){ return sum; }
float getAvg() { return avg; }
};
int main()
{
Student s1("kim",20,30);
Student s2("kang",10,50);
Student s3("lee",20,20);
cout << "총인원:" << s1.getCnt() <<endl;
}
Ver3
#include <iostream>
#include <ctime>
#include<string>
#include <fstream>
using namespace std;
class Person{
private:
string name;
static int cnt;
public:
Person(){cnt++;}
Person(string n){name=n;}
static int getCnt(){return cnt;}
void setName(string n){name=n;}
string getName(){return name;}
};
int Person::cnt=0;
class Student : public Person
{
private:
int quiz1, quiz2, sum;
float avg;
public:
Student(){}
Student(string n, int q1, int q2):Person(n){ quiz1=q1; quiz2=q2; }
void setQuiz(int q1, int q2){ quiz1=q1; quiz2=q2; }
void calcSumAvg(){sum=quiz1+quiz2; avg= sum/2.0;}
int getSum(){ return sum; }
float getAvg() { return avg; }
};
int main()
{
Student s[3];
s[0].setName("kim");
s[1].setName("kang");
s[2].setName("lee");
cout << "총인원:" << s[0].getCnt() <<endl;
}