Nesne Tabanli Temel bir C++ programi
----- Node.h dosyasi ABSTRACT CLASS-----
#ifndef _NODE_H_
#define _NODE_H
class Node{
private:
Node(Node & oldNode); //Disable copy
public:
Node(){};
virtual void dump() const = 0; //pure virtual
};
#endif
---SimpleNode.h-----
#ifndef _SIMPLENODE_H
#define _SIMPLENODE_H
#include "Node.h"
#include <iostream>
// Node'dan turetiliyor.
class SimpleNode: public Node{
private:
const std::string data;
public:
SimpleNode(const std::string s): data(s){
}
void dump() const;
};
#endif
---SimpleNode.cpp dosyasi----
#include "SimpleNode.h"
#include <iostream> //cout icin gerekli
using namespace std;
void SimpleNode::dump() const{
cout << data;
}
int main(){
SimpleNode sn("Tete"); //Bir nesne yaratiyor!
SimpleNode *ns = new SimpleNode("Bu new kullanir, isaretci gonderir");
//Node n1=sn; //Bu hata verir. Cunku Node bir abstract nesne ve yaratilamaz.
Node *n = &sn;
Node &n2 = sn; //Burada n2 sn icin bir lakab (alias)
(*n).dump(); //Asagida alternatif bir yontem var.
n->dump();
n2.dump();
}
#ifndef _NODE_H_
#define _NODE_H
class Node{
private:
Node(Node & oldNode); //Disable copy
public:
Node(){};
virtual void dump() const = 0; //pure virtual
};
#endif
---SimpleNode.h-----
#ifndef _SIMPLENODE_H
#define _SIMPLENODE_H
#include "Node.h"
#include <iostream>
// Node'dan turetiliyor.
class SimpleNode: public Node{
private:
const std::string data;
public:
SimpleNode(const std::string s): data(s){
}
void dump() const;
};
#endif
---SimpleNode.cpp dosyasi----
#include "SimpleNode.h"
#include <iostream> //cout icin gerekli
using namespace std;
void SimpleNode::dump() const{
cout << data;
}
int main(){
SimpleNode sn("Tete"); //Bir nesne yaratiyor!
SimpleNode *ns = new SimpleNode("Bu new kullanir, isaretci gonderir");
//Node n1=sn; //Bu hata verir. Cunku Node bir abstract nesne ve yaratilamaz.
Node *n = &sn;
Node &n2 = sn; //Burada n2 sn icin bir lakab (alias)
(*n).dump(); //Asagida alternatif bir yontem var.
n->dump();
n2.dump();
}
Etiketler: CPP
Toplam 0 Yorum:
Yorum Gönder
<< Ana Sayfa