일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 43. Multiply Strings
- DWG
- kaggle
- Python
- shiba
- t1
- Class
- 315. Count of Smaller Numbers After Self
- 운영체제
- LeetCode
- 프로그래머스
- attribute
- Regular Expression
- Generator
- 30. Substring with Concatenation of All Words
- 컴퓨터의 구조
- Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- data science
- Protocol
- 밴픽
- Python Code
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- concurrency
- 시바견
- Python Implementation
- iterator
- Decorator
Archives
- Today
- Total
Scribbling
[C++] Virtual Functions 본문
https://www.hackerrank.com/challenges/virtual-functions/problem?isFullScreen=true
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Person {
public:
string name;
int age;
Person() {}
virtual void getdata() {};
virtual void putdata() {};
virtual ~Person() {}
};
class Professor : public Person {
public:
static int id;
int cur_id;
int publications;
Professor() : Person() {
Professor::id++;
cur_id = Professor::id;
}
virtual void getdata() override {
string _name;
int _publications;
int _age;
cin >> _name;
cin >> _publications;
cin >> _age;
this->name = _name;
this->publications = _publications;
this->age = _age;
}
virtual void putdata() override {
cout << this->name << " ";
cout << this->publications << " ";
cout << this->age << " ";
cout << this->cur_id << endl;
}
};
class Student : public Person {
public:
static int id;
int cur_id;
int marks[6];
int marks_total {0};
Student() : Person() {
Student::id++;
cur_id = Student::id;
}
virtual void getdata() override {
string _name;
int _age;
cin >> _name;
cin >> _age;
this->name = _name;
this->age = _age;
for (int i=0; i<6; i++) {
cin >> marks[i];
marks_total += marks[i];
}
}
virtual void putdata() override {
cout << this->name << " ";
cout << this->age << " ";
cout << this->marks_total << " ";
cout << this->cur_id << endl;
}
};
int Professor::id {0};
int Student::id {0};
int main(){
int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];
for(int i = 0;i < n;i++){
cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;
}
else per[i] = new Student; // Else the current object is of type Student
per[i]->getdata(); // Get the data from the user.
}
for(int i=0;i<n;i++)
per[i]->putdata(); // Print the required output for each object.
return 0;
}
'Computer Science > C++' 카테고리의 다른 글
[C++] Strings (0) | 2024.09.30 |
---|---|
[C++] Abstract Class, Polymorphism (0) | 2024.09.28 |
[C++] Regular Expression Matching (0) | 2024.09.26 |
[C++] Priority Queue with custom data type (0) | 2024.02.22 |
[C++] Abstract Class, Interface, Multiple Inheritance (0) | 2024.02.14 |