2 Replies - 350 Views - Last Post: 03 March 2012 - 10:53 AM Rate Topic: -----

#1 Mandla Msibi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-April 11

Classes and inheritance

Posted 03 March 2012 - 02:53 AM

hi guys I'm a new c++ programmer and I'm trying to make a programme that:

• defines a class named Publication whose data members are title and medium and (the data item
medium should be initialized to the word paper The class’s operations are get title() and get medium().
• defines a class named Report that inherits all the properties of the Publication class and adds its
own data item named institute and data operation get institute()
• defines a third class named FTP (File Transfer Protocol), that inherits all the properties of the
class Report and adds its own data items site and medium and data operations get site() and
get medium().
• defines the function get medium()
• creates an object of the class FTP that changes the medium from paper to computer.

When I compile the programme I get the following errors and I'm not really sure what they mean:

strings.cpp:20: error: ‘Report’ has not been declared
strings.cpp:21: error: expected unqualified-id before ‘{’ token
strings.cpp:28: error: ‘FTP’ has not been declared
strings.cpp:29: error: expected unqualified-id before ‘{’ token
strings.cpp: In function ‘int main()’:
strings.cpp:42: error: no match for ‘operator<<’ in ‘std::cin << new_medium’
strings.cpp:43: error: ‘Public’ was not declared in this scope
student@pc533:~/Desktop/shared-folder$ g++ -o strings strings.cpp
strings.cpp:20: error: ‘Report’ has not been declared
strings.cpp:20: error: expected unqualified-id before ‘)’ token
strings.cpp:28: error: ‘FTP’ has not been declared
strings.cpp:29: error: expected unqualified-id before ‘{’ token
strings.cpp: In function ‘int main()’:
strings.cpp:42: error: no match for ‘operator<<’ in ‘std::cin << new_medium’
strings.cpp:43: error: ‘Public’ was not declared in this scope 


ANd the code is as follows
# include <iostream>
# include <string>
using namespace std;

class Publication
{
public:
	string get_medium();
	string get_title();
private:
	string medium;
	string tilte;
};


class Report::Publication()
{
private:
	string institute;
public:
	string get_institute();
};

class FTP::Report
{
private:
	string medium;
	string site;
public:
	string get_site();
	string get_medium();
};

int main()
{
	string new_medium;
	cout <<"Enter the name of the new medium "<<endl;
	cin <<new_medium;
	cout<< Public.get_medium(new_medium);
};



MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 March 2012 - 03:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Classes and inheritance

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Classes and inheritance

Posted 03 March 2012 - 03:03 AM

Code Tags Always help. =D

EDIT: Whoops didn't read that right. You are not declaring your second and third classes right. You are trying to declare them like methods. You need to declare them like classes.

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

class Publication
{
public:
string get_medium();
string get_title();
private:
string medium;
string tilte;
};


class Report::Publication()
{
private:
string institute;
public:
string get_institute();
};

class FTP::Report
{
private:
string medium;
string site;
public:
string get_site();
string get_medium();
};

int main()
{
string new_medium;
cout <<"Enter the name of the new medium "<<endl;
cin <<new_medium;
cout<< Public.get_medium(new_medium);
};

This post has been edited by jdavi134: 03 March 2012 - 03:13 AM

Was This Post Helpful? -1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 664
  • View blog
  • Posts: 2,277
  • Joined: 31-December 10

Re: Classes and inheritance

Posted 03 March 2012 - 10:53 AM

Quote

defines a class named Report that inherits all the properties of the Publication class and adds its
own data item named institute and data operation get institute()

This is not how you inherit from a class. Here is a simple example showing how you need to do it:
// inherit.cc
#include <iostream>
using namespace std;

class Base
{
public:
     Base() { cout << "Base::Base()\n"; }
     ~Base() { cout << "Base::~Base()\n"; }
};

class Derived : public Base
{
public:
     Derived() { cout << "Derived::Derived()\n"; }
     ~Derived() { cout << "Derived::~Derived()\n"; }
};

int main()
{
	Base b;
	Derived d;
}


Output of the above program:

Quote

./inherit
Base::Base()
Base::Base()
Derived::Derived()
Derived::~Derived()
Base::~Base()
Base::~Base()

Notice that even though I only instantiated 1 Base class object and 1 Derived class object that there is an extra Base object printed out. This is because the 'd' object contains a Base subobject as well as anything that is defined for the Derived class.

So when the constructor for the Derived class gets called, it actually calls the Base class constructor first, then the Derived class constructor.

This post has been edited by vividexstance: 03 March 2012 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1