Ép kiểu cho con trỏ trong kế thừa
Trang 1 trong tổng số 1 trang
Ép kiểu cho con trỏ trong kế thừa
Thằng con kế thừa từ thằng cha, nhưng mà thằng cha lại không thể dùng con trỏ để truy nhập đến thằng con. Muốn điều đó xảy ra, thì phải ép con trỏ.
Code về ép con trỏ như sau
File diem.h
File diem.cpp
File Duongtron.h
File duongtron.cpp
File thihanh.cpp
Code về ép con trỏ như sau
File diem.h
[You must be registered and logged in to see this link.]
//dinh nghia lop Diem
#ifndef DIEM_H
#define DIEM_H
class Diem
{
protected:
float x,y;
public:
Diem (float a=0, float b=0);
void THDiem(float a, float b);
float TDoX() const
{
return x;
}
float TDoY() const
{
return y;
}
friend ostream & operator << (ostream & out, const Diem &p);
};
#endif
File diem.cpp
[You must be registered and logged in to see this link.]
//Dih nghia cac ham thanh vien cua lop Diem
#include "iostream.h"
#include "diem.h"
Diem::Diem(float a, float b)
{
THDiem(a,b);
}
void Diem::THDiem(float a, float b)
{
x = a;
y = b;
}
ostream & operator <<(ostream &out, const Diem &p)
{
out << '[' << p.x << ", " << p.y << ']';
return out;
}
File Duongtron.h
[You must be registered and logged in to see this link.]
//dinh nghia lop duongtron
#ifndef DUONGTRON_H
#define DUONGTRON_H
#include "diem.h"
class DuongTron : public Diem
{
protected:
float bankinh;
public:
DuongTron(float r=0.0, float a=0, float b=0);
void KTBK(float r);
float LayBK() const;
float DienTich() const;
friend ostream & operator <<(ostream & Output, const DuongTron &C);
};
#endif
File duongtron.cpp
[You must be registered and logged in to see this link.]
//Dinh nghia cac ham thanh vien cua lop BanKinh.cpp
#include "iostream.h"
#include "iomanip.h"
#include "duongtron.h"
DuongTron::DuongTron(float r, float a, float b): Diem(a, b)
{
bankinh = r;
}
void DuongTron::KTBK(float r)
{
bankinh = r;
}
float DuongTron::LayBK() const
{
return bankinh;
}
float DuongTron::DienTich() const
{
return 3.14159 * bankinh * bankinh;
}
//Xuat duongtron theo dang: ToaDoTam = [x, y]; Bankinh = #.##
ostream & operator <<(ostream &Output, const DuongTron &C)
{
Output << "Toa Do Tam = [" << C.x << ", " << C.y
<< "]; Ban Kinh = " << setiosflags(ios::showpoint)
<< setprecision(2) << C.bankinh;
return Output;
}
File thihanh.cpp
//Chuong trinh thi hanh
//Ep cac con tro lop co so toi con tro lop dan xuat
#include "iostream.h"
#include "iomanip.h"
#include "diem.h"
#include "diem.cpp"
#include "duongtron.h"
#include "duongtron.cpp"
void main()
{
Diem *pt, P(3.5, 5.3);
DuongTron *cp, C(2.7, 1.2, 8.9);
cout << "Diem P: "< //Xu ly 1 duong tron nhu 1 diem, chi xem 1 phan lop co so
pt = &C;
cout << endl << "Duong Tron C (va *pt): "<<*pt<
pt = &C;
cp = (DuongTron *) pt;
cout << endl << endl
<<*cp<< endl
<< cp->DienTich() << endl;
pt = &P;
cp = (DuongTron *) pt;
cout << endl
<<*cp<< endl
<DienTich() << endl;
}
Similar topics
» Hỏi về 1 số kiểu trong Power Designer
» Tìm hiểu cây 2 - 3 - 4
» Kế thừa? Kế thừa đơn? Hàm hủy, hàm tạo?
» wow, kiểu áo đẹp quá
» Kiếm tiền kiểu này hay và thiết thực nhỉ
» Tìm hiểu cây 2 - 3 - 4
» Kế thừa? Kế thừa đơn? Hàm hủy, hàm tạo?
» wow, kiểu áo đẹp quá
» Kiếm tiền kiểu này hay và thiết thực nhỉ
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|