01/10/2018, 09:02

Function is inaccessible

Em vừa đọc được một vài tài liệu về lập trình hướng đối tượng, khi em bắt tay vào làm bài tập thì bị lỗi

sv.h: In function ‘int main()’:
sv.h:13:7: error: ‘void nguoi::datTen(std::__cxx11::string)’ is inaccessible
  void datTen(string name) {
       ^

Đây là file .h của em:

#include <iostream>
#include <string>
using namespace std;

class nguoi {
public:
	string layTen() {
		return ten;
	}
	int layTuoi() {
		return tuoi;
	}
	void datTen(string name) {
		ten = name;
	}
	void datTuoi(int age) {
		tuoi = age;
	}
private:
	int tuoi;
	string ten;
};
class sinhVien: private nguoi{
public:
	float layDiemTB() {
		return diemTB;
	}
	void datDiemTB(float mark) {
		diemTB = mark;
	}

private:
	float diemTB;
};

đây là file .cpp:

#include <iostream>
#include <string>
#include "sv.h"

using namespace std;



int main() {
	sinhVien sv;
	string name;
	int age;
	float mark;
	cout<<"Nhap ten sinh vien : ";
	cin>>name;
	sv.datTen(name);
	cout<<"Nhap tuoi sinh vien : ";
	cin>>age;
	sv.datTuoi(age);
	cout<<"Nhap diem tb cua sinh vien : ";
	cin>>mark;
	sv.datDiem(mark);

	cout<<"Sinh vien "<<sv.layTen()<<" "<<sv.layTuoi()<<" co "<<sv.layDiem()<<" diem.
";

	return 0;
}

vì mới học được có 2 ngày nên đầy óc còn ngu muội nhưng thật sự em không biết mình sai chỗ nào ạ, mong anh chị giúp đỡ .

Tiện thể cho em xin một vài quyển sách/tài liệu LT HDT hay đi ạ, em đang rất hứng thú với cái này nhưng mà trên mạng nhiều tài liệu quá không biết chon cái nào

tan_viet viết 11:04 ngày 01/10/2018

màn hình lỗi đầy đủ:

In file included from sv.cpp:3:0:
sv.h: In function ‘int main()’:
sv.h:13:7: error: ‘void nguoi::datTen(std::__cxx11::string)’ is inaccessible
  void datTen(string name) {
       ^
sv.cpp:16:16: error: within this context
  sv.datTen(name);
                ^
sv.cpp:16:16: error: ‘nguoi’ is not an accessible base of ‘sinhVien’
In file included from sv.cpp:3:0:
sv.h:16:7: error: ‘void nguoi::datTuoi(int)’ is inaccessible
  void datTuoi(int age) {
       ^
sv.cpp:19:16: error: within this context
  sv.datTuoi(age);
                ^
sv.cpp:19:16: error: ‘nguoi’ is not an accessible base of ‘sinhVien’
sv.cpp:22:5: error: ‘class sinhVien’ has no member named ‘datDiem’
  sv.datDiem(mark);
     ^
In file included from sv.cpp:3:0:
sv.h:7:9: error: ‘std::__cxx11::string nguoi::layTen()’ is inaccessible
  string layTen() {
         ^
sv.cpp:24:32: error: within this context
  cout<<"Sinh vien "<<sv.layTen()<<" "<<sv.layTuoi()<<" co "<<sv.layDiem()<<" di
                                ^
sv.cpp:24:32: error: ‘nguoi’ is not an accessible base of ‘sinhVien’
In file included from sv.cpp:3:0:
sv.h:10:6: error: ‘int nguoi::layTuoi()’ is inaccessible
  int layTuoi() {
      ^
sv.cpp:24:51: error: within this context
  cout<<"Sinh vien "<<sv.layTen()<<" "<<sv.layTuoi()<<" co "<<sv.layDiem()<<" di
                                                   ^
sv.cpp:24:51: error: ‘nguoi’ is not an accessible base of ‘sinhVien’
sv.cpp:24:65: error: ‘class sinhVien’ has no member named ‘layDiem’
  cout<<"Sinh vien "<<sv.layTen()<<" "<<sv.layTuoi()<<" co "<<sv.layDiem()<<" di
                                                                 ^
viết 11:09 ngày 01/10/2018

class sinhVien: private nguoi{

SinhVien phải kế thừa public từ lớp Nguoi: class SinhVien : public Nguoi{

trong C++ có kế thừa private, protected, public, đa số toàn kế thừa public, 2 cái kia ít xài.

tan_viet viết 11:18 ngày 01/10/2018

Em chạy được rồi ạ, nhưng mà anh có thể nói thêm khi nào kế thừa public, khi nào private, khi nào protected đi ạ

Bài liên quan
0