ThienThanCNTT
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Ép kiểu cho con trỏ trong kế thừa

Go down

Ép kiểu cho con trỏ trong kế thừa Empty Ép kiểu cho con trỏ trong kế thừa

Bài gửi by nth 02/08/09, 05:02 pm

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

[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;

}

nth
nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 35
Đến từ : Thiên Đường

https://thuhuong.forumvi.net

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết