Creating Abstract Data Types

Hello!

I am having problems understanding how to create my own abstract data types.

This is the question in which it is taken out of,

Write a program to manage DVD rental in a video rental store. Create an abstract data type that represents a DVD in this store. Consider all the data and operations that may be necessary for the DVD type to work well within a rental management system. Include a print() member function that displays all the information about the DVD. Test your data type by creating an array of ten DVD instances and filling them using information read from a test input file that you create. Display the DVD information.

I just need help on how to set up an abstract data type how to get it to read individual sections in an input file.

Thank you

An Abstract Base Class (ABC) has pure virtual methods which are implemented in a derived class. Your problem description makes no mention of any derived classes.

It's not clear from the problem description if "abstract data type" really means a true Abstract Base Class, or is simply referring to a class that is an "abstraction" for a DVD. If what was meant was an "abstraction" of a DVD, then we're talking about a simple class.
Ahhh ok, that makes sense!
So would something like this work for a DVD Class?

1
2
3
4
5
6
7
class dvd
{
  public:
    string name;
    string type;
    string description;
};
That's a start for your data members.

Now you need to think about member functions.
How would I go about storing everything from a textfile then displaying it? Or am I missing something here. I'm having the impression that I have to promp the program to fill everything in on the dvd program, then display it as an array while having a dvd class.
closed account (E0p9LyTq)
It isn't listed in your requirements, but the only prompt I can think of you need to have is asking the user the name/location of the data file.

If you hardcode the data file name, then you should only see output from the file in the console window.

The output generated from the data you read in from the file, using your print() member function you write.
One way is to have a Read function.

Here's a very simple Read function.
1
2
3
4
5
6
7
 
//  Read one DVD from a file.
//  Return false at EOF
bool dvd::Read (istream & is)   
{  is >> name >> type >> description;
    return is.good();
}


This function is limited to reading one word for each of the fields (>> stops at whitespace).
You should look at getline() if you want to read multiple words into a string.
If you use getline, you will need a delimiter between each field.

Last edited on
Alright, so this is what I have so far. There is of course a couple mistakes in the class file, but it is all I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include "dvd.cpp"
#include <stdio.h>
#include <fstream>
#include <sstream>
using namespace std;


int main()
{
    string dvdarray[10];
    string filename = "TMA2Q1.txt";

    ifstream f("TMA2Q1.txt");

    cout << "Press 'enter' to display a line:" << endl;
    cin.get();

    for(int x=0; x<10; x++)
    {
        getline(f, dvdarray[x]);
        cout << "Line " << x << ": " << dvdarray[x] << endl;
        cin.get();
    }
    return 0;
}


Here's my class file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef DVD_H
#define DVD_H
#include <string>


class dvd
{
  public:
      void dvdName(){
      }

      void dvdGenre(){
      }

      void dvdDescription(){
      }

      void dvdRent(){
      }

      void dvdReturn(){
      }
      dvd();
  protected:
  private:
      std::string getName()
      {
          return name;
      }
      std::string getGenre()
      {
          return genre;
      }
      std::string getRentdate()
      {
          return rentdate;
      }
      std::string getRentreturn()
      {
          return rentreturn;
      }
      std::string getRating()
      {
          return rating;
      }
};

#endif // DVD_H 


I'm unsure on how to put the individualized information per DVD in each array. For say, every array has a different DVD layout and can sort it out according to the text file.
Last edited on
closed account (E0p9LyTq)
DVD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef DVD_H
#define DVD_H

#include <string>

class DVD
{
   // ctors and dtor
public:
   // default ctor, data members set to default (maybe blank "") values
   DVD();

   // ctor for creating a DVD object with known values
   DVD(std::string /*title*/,
       std::string /*genre*/,
       std::string /*description*/,
       std::string /*checkout_date*/,
       std::string /*checkin_date*/,
       std::string /*rating*/ );
   ~DVD() {}  // nothing special needed for the dtor

      // getters
public:
   std::string getTitle() const       { return itsTitle; }
   std::string getGenre() const       { return itsGenre; }
   std::string GetDescription() const { return itsDescription;  }
   std::string getRentDate() const    { return itsDate_rented; }
   std::string getRentReturn() const  { return itsDate_returned;}
   std::string getRating() const      { return itsRating; }

   // setters needed so you can change something if needed
   // one example, you can hopefully figure out the rest :)
   void setTitle(std::string title)  { itsTitle = title; }

   // data members
private:
   std::string itsTitle;
   std::string itsGenre;
   std::string itsDescription;
   std::string itsDate_rented;
   std::string itsDate_returned;
   std::string itsRating;
};

#endif 


a possible DVD.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include "dvd.h"

DVD::DVD()
{
   itsTitle = "";
   itsGenre = "";
   itsDescription = "";
   itsDate_rented = "";
   itsDate_returned = "";
   itsRating = "";
}


DVD::DVD(std::string title,
         std::string genre,
         std::string description,
         std::string checkout_date,
         std::string checkin_date,
         std::string rating )
{
   itsTitle = title;
   itsGenre = genre;
   itsDescription = description;
   itsDate_rented = checkout_date;
   itsDate_returned = checkin_date;
   itsRating = rating;
}


class test engine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "DVD.h"

int main()
{
   const unsigned int number_of_dvds = 10;

   // create an array of DVDs on the heap and instantiate them
   DVD* dvds = new DVD[number_of_dvds];

   // was the array created properly? let's check the 5th DVD (element 4)
   std::cout << "The 5th DVD's title: " << dvds[4].getTitle() << "\n";

   for (unsigned int loop = 0; loop < number_of_dvds; loop++)
   {
      // do all the work of adding the DVD information
      // adding just the title to show how to do it
      dvds[loop].setTitle("Movie #" + std::to_string(loop));
   }

   // let's check the title was updated
   std::cout << "The 5th DVD's title: " << dvds[4].getTitle() << "\n";

   // delete the DVD array on the heap when done
   // it pays to be neat!
   delete[] dvds;
}


The 5th DVD's title:
The 5th DVD's title: Movie #4


This is simply a bare minimum needed class layout, just to point you in the right direction. People can easily pick it apart as being very incomplete and error prone.
Alright, so I did some changes but i'm getting compiler errors. Additionally, i'm really not sure how the program seperates all of the text in the textfile into a DVD class (that's what is really scratching my head, I'm rather confused). Also, it seems you used pointers in the class test engine, so i'll have to check out how those work. Here's what I have so far,

dvd.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef DVD_H
#define DVD_H

#include <string>

class DVD
{
   // ctors and dtor
public:
   // default ctor, data members set to default (maybe blank "") values
   DVD();

   // ctor for creating a DVD object with known values
   DVD(std::string title; // I am getting errors here, and i'm not entirely sure why (protoype for 'DVD::DVD(std
       std::string genre;
       std::string description;
       std::string rentdate;
       std::string datereturn;
       std::string rating;
   ~DVD() {}  // nothing special needed for the dtor

      // getters
public:
   void setTitle(std::string title)
   {
       itsTitle = title;
   }
   std::string getTitle() const
   {
       return itsTitle;
   }

   void setGenre(std::string genre)
   {
       its Genre = genre
   }
   std::string getGenre() const
   {
       return itsGenre;
   }

   void setDescription(std::string genre)
   {
       itsDescription = description;
   }
   std::string GetDescription() const
   {
       return itsDescription;
   }

   void setRentDate(std::string checkout_date)
   {
       itsDate_rented = checkout_date;
   }
   std::string getRentDate() const
   {
       return itsDate_rented;
   }


   void setDateReturn(std::string checkin_date)
   {
       itsDate_returned = checkin_date;
   }
   std::string getRentReturn() const
   {
       return itsDate_returned;
   }

   void setRating(std::string rating)
   {
       itsRating = rating;
   }
   std::string getRating() const
   {
       return itsRating;
   }

   // setters needed so you can change something if needed
   // one example, you can hopefully figure out the rest :)

   // data members
private:
   std::string itsTitle;
   std::string itsGenre;
   std::string itsDescription;
   std::string itsDate_rented;
   std::string itsDate_returned;
   std::string itsRating;
};

#endif 


dvd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "dvd.h"
#include <iostream>

using namespace std;


DVD::DVD()
{
   itsTitle = "";
   itsGenre = "";
   itsDescription = "";
   itsDate_rented = "";
   itsDate_returned = "";
   itsRating = "";
}


DVD::DVD(std::string title,
         std::string genre,
         std::string description,
         std::string checkout_date,
         std::string checkin_date,
         std::string rating )
{
   itsTitle = title;
   itsGenre = genre;
   itsDescription = description;
   itsDate_rented = checkout_date;
   itsDate_returned = checkin_date;
   itsRating = rating;
}


Class test engine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include "dvd.cpp"
#include <stdio.h>
#include <fstream>
#include <sstream>
using namespace std;


int main()
{
    string DVD[10];
    string filename = "TMA2Q1.txt";

    ifstream f("TMA2Q1.txt");

    cout << "Press 'enter' to display a line:" << endl;
    cin.get();

    for(int x=0; x<10; x++)
    {
        getline(f, DVD[x]);
        cout << "Line " << x << ": " << DVD[x] << endl;
        cin.get();
    }
    return 0;
}
dvd.h
------
Lines 14-19: The arguments to the constructor need to be separated by , not ;
Line 19: You need to close the declaration of the constructor with a )
Line 22: Your comment is misleading. You're mixing your setters and getters together.
Line 35: Extraneous space in itsGenre. Missing ;
Line 42: Argument name incorrect.

dvd.cpp
-------
Lines 14-19: These assignments are not needed. A std::string is implicitly initialized to an empty string.

main.cpp
--------
Line 2: Never include a .cpp file. dvd.cpp should be included as part of your project and separately compiled.

Line 3: Correct header is <cstdio>

Line 11: You're creating any array of 10 strings. You want an array of 10 DVD's.
 
  DVD dvds[10];  // A std::vector would be a better choice if you've learned vectors 


Line 21: getline doesn't know how to get a DVD. You need a read function as I suggested earlier. It would be helpful if you posted the format of your input file.

Line 22: cout does not know how to display a DVD. You either need a print function, or overload the << operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//  Read one DVD from the input file
//  Assumes one field per line in input file
bool DVD::Read (istream & is)
{   getline (is, itsTitle);
    getline (is, itsGenre);
    getline (is, itsDescription);
    getline (is, itsDate_rented);
    getline (is, itsDate_returned);
    getline (is, itsRating);
    return is.good();
}

void DVD::Print (ostream & os) const
{   os << "Title: " << itsTitle << endl;
    os << "Genre: " << itsGenre << endl;
    os << "Description: " << itsDescription << endl;
    os << "Date Rented: " << itsDate_rented << endl;
    os << "Date Returned: " << itsDate_returned << endl;
    os << "Rating: " << itsRating << endl;
}


Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/195739/#msg940923
Ahhhhhh ok! That kind of makes sense! I'm gonna change a couple things and get back to you. Thanks Anon
I'm about to give up, I can't do this. I'm so lost in this course, and it's my second programming course i've ever taken. I'm running out of time to finish all my assignments as well, and i'm not efficient what so ever at this. I'm also taking this course online, I honestly don't know what to do. Probably gonna have to retake it a second time.
Saying "I give up. I can't do this" isn't helpful.

We haven;t heard from you in a week. What have you been doing?
Did you make the changes I pointed out above?
What does your current code look like?
LiBRiUMz
Do not give up so quick. First pick apart what the directions say.

"Create an abstract data type that represents a DVD in this store."


That says to me start with;
1
2
3
class DVD
{
}


Within the class you will need your private variables and public member functions. One member you can get from the instructions is a print function. Last the instructions tell you to create an array of 10 instances of the DVD class, and then fill the array with information read in from a file that you create, once the file is read print it out.

Once you know what to work on it becomes easier to work on small pieces instead of the whole. Expanding on the genre and rating you could use an enumeration for these variable which would take up less space in a file. Later in the print function use a switch case to chance the numbers into something meaningful.

Here is an example of my interpretation of the directions. May not be the best way to code the program, but it is quick and dirty and food for thought.

The DVD.h file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once

class DVD
{
public:
	// constructor and destructor defined outside of the class
	DVD();
	~DVD();
	// Forward declarations defined outside of class in the main file
	void GetInputFromFile(ifstream &IF);
	void PrintRec();


private:
	string m_tital;
	string m_genra;
	string m_rating;
	int m_year;
	string m_rentalDate;
	string m_returnDate;
};

// constructor setting all the private varables to empty or 0

DVD::DVD() : m_tital{ "" }, m_genra{ "" }, m_rating{ "" }, m_year{ 0 }, m_rentalDate{ "" }, m_returnDate{ "" }
{
}

// destructor
DVD::~DVD()
{
}


The Main file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//  DVD Rental.cpp : Defines the entry point for the console application.
//  Written and compiled with Visual Studio 2015 with Windows 10 OS
//

#include "stdafx.h"
#include <stdhead.h>  // my own file contains "iostream", "string", "cstring", "vector", "iomanip", "Windows.h" just saves me some typing
#include <fstream>  // for file IO
#include <array>   //  for use of the array class

#include "proto.h"  // A file for my own use

#include "DVD.h"

//  Member fuctions of the DVD class
void DVD::GetInputFromFile(ifstream &IFILE)
{
	IFILE >> m_genra >> m_rating >> m_year >> m_rentalDate >> m_returnDate >> m_tital;
}

void DVD::PrintRec()
{
		cout << m_tital << endl << m_genra << endl << m_rating << endl << m_year << endl << m_rentalDate << endl << m_returnDate << endl;
}


int main()
{
	system("color 17");  // This and line 29 not needed for the program my personal use

	CLS;


	ifstream IFILE;  // define the input file type
	IFILE.open("Rental Info.txt");
	
	//  Test for is open
	if (IFILE.is_open())
	{
		cout << "File is open.\n" << endl;
	}
	else
	{
		CLS;
		cout << "\n\nCan not open \"Rental Info.txt\"" << endl;
		Sleep(5000);
		exit(1);
	}

	array<DVD, 10> myarray;  // Create an array of 10 DVDs

//  Could be done as a DVD member function,but worked for a quick solution
	for (int lp = 0; lp < myarray.size(); lp++)
	{
		myarray[lp].GetInputFromFile(IFILE);
	}
	
	for (int lp = 0; lp < myarray.size(); lp++)
	{
		myarray[lp].PrintRec();
		cout << endl;
	}

	cout << "\n\n\n\nFin Press any key to continue -- > ";
	_getch();

	IFILE.close();

	return 0;
}
 


And the test file I used to read in the information.

1
2
3
4
5
6
7
8
9
10
11
music NR 2004 11/12/2016 11/15/2016 Celtic_Woman_a_New_Journey
music NR 2006 12/20/2016 12/25/2016 Celtic_Woman_a_New_Beginning
music NR 2007 10/15/2016 10/20/2016 Chloe
SIFI PG 2000 08/02/2016 08/07/2016 Startrek_Voyager_Seson_3
SIFI PG 2000 08/09/2016 08/14/2016 Startrek_Voyager_Seson_4
SIFI PG 2000 08/09/2016 08/14/2016 Startrek_Voyager_Seson_5
SIFI PG 2000 08/09/2016 08/14/2016 Startrek_Voyager_Seson_6
Drama PG 1999 07/01/2016 07/08/2016 Criminal_Minds_Season_1
Drama PG 1999 07/01/2016 07/08/2016 Criminal_Minds_Season_2
Drama PG 1999 07/01/2016 07/08/2016 Criminal_Minds_Season_3
 


Notice the way I wrote the title or name of the dvd. I did this based on the way of reading input from the file. Input stops on a white space, so you can not have any white space in the title. A DVD member function could be written to remove the underscore characters and replace them with spaces.

I hope this helps you understand this better.

Andy
Hi Andy, I appreciate the time you put into writing all that up, however, a lot of the syntax I don't quite recognize. It seems that everyone uses their own syntax in C++ and it makes it rather confusing.

I decided to start over, and this is what I have. I wrote down the errors I was getting, and would like some help getting some resolved. Another issue, is how do I read from the file and fill in the arrays respectively?

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class DVD{
    public:
        void setproductName (string z) {
            productName = z;
        }
        string getproductName(){
            return productName;
        }

        void setmovieGenre (string w) {
            movieGenre = w;
        }
        string getmovieGenre (){
            return movieGenre;
        }
        void setclient (string v){
            client = v;
        }
        string getclient (){
            return client;
        }
        void setdateRented (string u) {
            dateRented = u;
        }
        string getdateRented (){
            return dateRented;
        }
        void setrentReturn (string t) {
            rentReturn = t;
        }
        string getrentReturn (){
            return rentReturn;
        }
    private:
        string productName = "Title: "; //Got a "warning: non-static data member initializers only available with -std=c++11
        string movieGenre = "Genre: ";
        string client = "Client Name: ";
        string dateRented = "Date Rented: ";
        string rentReturn = "Date Returned: ";

 };

 int main()
{
    DVD a[10];
    int i;

    for(i = 0; i<10; i++)//I don't understand how to overload the operator. Can someone break it down simply, set by step?
    cout << a[i].setproductName(i) << endl;//Error here, invalid conversin from 'int' to 'const char*' [-fpermissive]
    cout << a[i].getmovieGenre(i) << endl;
    cout << a[i].getclient(i)<< endl;
    cout << a[i].getdateRented(i) << endl;
    cout << a[i].getrentReturn(i) << endl;

    return 0;
 }
Lines 40-44: As the error says, you need to turn on the -std=C++11 compiler flag. Or get rid of the initializers. std::string defaults to any empty string, so you really don't need to initialize them.

Line 53: You need to initialize the data in your array of DVDs somehow. Previously you wanted to read this from a file. I gave you a read() routine in an earlier post in this thread.
http://www.cplusplus.com/forum/beginner/195459/#msg940210

Lines 53-58: If you want the for loop to apply to each of the cout statements, you need {} around the statements that are to be part of the for loop. As you have it, line 54 will execute 10 times, then lines 55-58 will execute once, but i will be out of bounds.

Lines 54-58: These members are private. You can't cout them here. See the print() function I provided earlier.

Don't worry about overloading the input or output operators. You're not ready for that yet.
Use the Read() and Print() functions I provided.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class DVD
{       string productName; 
        string movieGenre;
        string client;
        string dateRented;
        string rentReturn;
public:
        void setproductName (string z) {
            productName = z;
        }
        string getproductName(){
            return productName;
        }

        void setmovieGenre (string w) {
            movieGenre = w;
        }
        string getmovieGenre (){
            return movieGenre;
        }
        void setclient (string v){
            client = v;
        }
        string getclient (){
            return client;
        }
        void setdateRented (string u) {
            dateRented = u;
        }
        string getdateRented (){
            return dateRented;
        }
        void setrentReturn (string t) {
            rentReturn = t;
        }
        string getrentReturn (){
            return rentReturn;
        }   
        
    //  Read one DVD from the input file
    //  Assumes one field per line in input file
    bool Read (istream & is)
    {   getline (is, productName);
        getline (is, movieGenre);
        getline (is, client);
        getline (is, dateRented);
        getline (is, rentReturn);
        return is.good();
    }
    
    //  Print one DVD
    void Print (ostream & os) const
    {   os << "Product: " << productName << endl;
        os << "Genre: " << movieGenre << endl;
        os << "Client: " << client << endl;
        os << "Date Rented: " << dateRented << endl;
        os << "Date Returned: " << rentReturn << endl;
    }

 };

 int main()
{   DVD a[10];
    ifstream    dvdfile ("dvds.txt");    //  Declare and open the file of DVDs
    
    for(int i = 0; i<10; i++)
        a[i].Read (dvdfile);        //  Read one DVD from the file
    for(int i = 0; i<10; i++)   
        a[i].Print (cout);          //  Print one DVD 
    system ("pause");
    return 0;
 }




Last edited on
Hey Abstract, that bool function with the print one actually makes sense now. I didn't full understand how it worked, but now I do. Thank you. Thank you everyone for being so patient with my C++ beginner stupidity, and helping me finish this program. Thank you everyone, program works now.

Cheers!
Topic archived. No new replies allowed.