[C++]상속에 관련된 질문입니다(if사용)

p4r4digM의 이미지

부모 클래스를 만들고(ex. 교수클래스)

자식 클래스들로 전임교수클래스와 비전임교수클래스를 만들때

부모클래스(교수클래스)에서의 맴버 변수 중 전임/비전임을 구분하는 변수값으로

자식 클래스를 구분해서 바로 만들 수 있을까요??

즉, bool check라는 부모클래스의 맴버변수가 있으면

if(check){
전임교수클래스생성
}
else
비전임교수클래스생성

이러한 것은 가능할런지요.
정말 되는지 안되는지 궁금하네요.
숙제 때문에 시작한 고민이지만 되면 상당히 괜찮을 것 같다는 생각이드네요 :)

많은 그리고 빠른(^^;)답변 부탁드려요~

semmal의 이미지

#include <iostream>
#include <string>
#include <algorithm>
 
#include <cstdlib>
 
#define interface class
#define method virtual
#define pure = 0
 
using namespace std;
 
interface Prof {
public : method string check() pure;
};
 
class FullProf : public Prof {
public : method string check() {
  return "fullProf";
  }
};
 
class NonFullProf : public Prof {
public : method string check() {
    return "nonFullProf";
  }
};
 
void print(Prof* prof) {
  cout << prof->check() << endl;
}
 
int main(int argc, char* argv[]) {
  Prof* profs[] = { new FullProf(), new NonFullProf() };
 
  for_each(profs, profs+2, print);
 
  return EXIT_SUCCESS;
}

------------------------------
How many legs does a dog have?

------------------------------
How many legs does a dog have?

p4r4digM의 이미지

check가 메소드가 아니라 맴버변수인데...
저걸 다시 활용하면 가능하겠네요^^

역시 책을보고 이해하고 넘어간 것과
실질적으로 필요할때 써먹는 것과는 많은 차이가 있는거 같네요

소중한 답변 감사드립니다~~

앞으로는 실용적인 지식을 쌓아가야겠네요

semmal의 이미지

설령 프로퍼티로 해결할 수 있다고 하더라도 메소드를 써서 하는 버릇을 들이는게 좋습니다. 프로퍼티는 오브젝트에서만 쓰도록 하구요.
메소드라는 것은 클래스가 가지는 메시지를 정의하는 것이기 때문에 해당 클래스 전체가 같은 메소드를 써야한다면, 특히 이렇게 해결할 경우는, 프로퍼티는 쓸 수가 없습니다. 반대로 어떤 문제를 프로퍼티로 쉽게 해결할 수 있다고 하면, 상속을 할 이유가 없다는 뜻도 될 수 있습니다. 물론 언제나 그렇다고 하기에는 변수가 너무 많지만요.
------------------------------
How many legs does a dog have?

------------------------------
How many legs does a dog have?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.