Thread: printing out subclass objects and int sega int nintendo bool enjoy and string fun

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    37

    printing out subclass objects and int sega int nintendo bool enjoy and string fun

    how can I do that? please help

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    template<class gamer>
    class game
    {
    private:
    
    protected:
    	int nintendo;
    	int sega;
    	string fun;
    	bool enjoy;
    public:
    
    };
    
    template<class gamer>
    class cookies : public game<gamer> {
    public:
    	cookies() {}
    	cookies(std::string description)
    		: description(description)
    	{ }
    
    	std::string description;
    };
    int main()
    {    
    	template<class gamer>
    	std::vector<cookies> vec(5);
    	template<class gamer>
    	vec[0] = cookies("Tasty");
    	template<class gamer>
    	vec[1] = cookies("Delicious");
    	template<class gamer>
    	vec[2] = cookies("Yummy");
    	template<class gamer>
    	vec[3] = cookies("Hungry");
    	template<class gamer>
    	vec[4] = cookies("Full");
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your code makes no sense to me at all. What on earth does a game class template model, and why does it have those four member variables? What does it all mean?

    My suggestion is that you stop defining class templates. You almost certainly don't need it here. When you define your base class, think about what does it model, and declare the appropriate virtual member functions.

    Here's a contrived example of "printing out subclass objects":
    Code:
    #include <iostream>
    #include <ostream>
    #include <vector>
    
    class A
    {
    public:
        virtual void print(std::ostream& out) const
        {
            out << "This is an object of class A.";
        }
    
        virtual ~A() {}
    };
    
    std::ostream& operator<<(std::ostream& out, const A& obj)
    {
        obj.print(out);
        return out;
    }
    
    class B : public A
    {
    public:
        virtual void print(std::ostream& out) const
        {
            out << "This is an object of class B, derived from class A.";
        }
    };
    
    class C : public A
    {
    public:
        virtual void print(std::ostream& out) const
        {
            out << "This is an object of class C, derived from class A.";
        }
    };
    
    int main()
    {
        using namespace std;
    
        A a;
        B b;
        C c;
        vector<A*> items;
        items.push_back(&c);
        items.push_back(&b);
        items.push_back(&a);
        for (auto item : items)
        {
            cout << *item << endl;
        }
        // or if you cannot use the above loop:
        //for (vector<A*>::iterator iter = items.begin(); iter != items.end(); ++iter)
        //{
        //    cout << **iter << endl;
        //}
    }
    Notice that I declared A's destructor to be virtual, which is not important here but could matter when you use new and delete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    i have to use template for the assignment

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sonicflare9
    i have to use template for the assignment
    What exactly is your assignment, and why do you have to use a class template?

    As it stands, it looks like you are not using the template meaningfully, so even if your assignment calls for the use of a template, if I were your instructor I would most certainly not be awarding you any marks merely for having random template syntax thrown into your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An Idiot's Guide to C++ Templates - Part 1 - CodeProject
    Perhaps you should read more, rather than trying to learn through random typing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    37

    Classes & Objects
    (Due
    Tuesday
    , April
    24
    th
    at 11:59pm )
    The goal
    of this assignment is to build an application that consists of
    a class
    whose object values are assigned from a text fil
    e
    .
    There will also be a subclass,
    a vector and exception handling.
    Include the following criteria:
    Base Class
    You are going to create a base class with the following criteria:
    1)
    Us
    es
    class templates so that the class can be used in
    to handle
    different
    data
    type
    as required
    .
    2)
    Name the class whatever you like

    so long as it’s
    a valid identifier and it’s
    not offensive.
    It doesn’t have to represent anything but you’re welcome to
    come up with a theme if you like.
    3)
    The class will have the
    following four protected
    member variables
    named
    anything you like:
    a.
    A string
    b.
    Could be an int, float, or double
    c.
    Could be an int, float, or double
    d.
    A bool
    4)
    You will get the values for these member variables from a text file you
    create. The details of the text file are as follows:
    a.
    Name
    the text file whatever you like
    b.
    You must have five groups of the above base class members
    meaning there will be 20 items in the text file
    each group occupies
    a line in the text file (5 lines for 5 groups)
    .
    i.
    It is
    recommend
    ed
    that you put each “record” of fo
    ur on its
    own line for clarity purposes.
    ii.
    For example, sample lines
    in the file
    would be:
    Pie 3.1415 69 true
    Apple 4.234 42 true
    5)
    Now, you are going to create a method in the base class that prints those
    four members. So you’ll be required to set those mem
    bers from the file
    BEFORE you call the print function. More on this when the main() for the
    program
    will be explained
    .
    Subclass
    You’re also going to have to make a subclass (derived class) of your base class
    .
    Follow the criteria below:
    1)
    Name this class wh
    atever you like. You can make the name related to
    the base class if you like.
    2)
    Inherit
    this class publically from the base class.
    3)
    This class will only have ONE new
    private
    member variable and let’s
    make it another string with a name of your choice.
    4)
    You will
    NOT be getting the value of this string from the text file but
    rather you can set the string to whatever you like when you create an
    object of this subclass.
    a.
    That is, make a non
    -
    default constructor for this subclass with a
    string as its only parameter and
    then set the member string to
    the value held in the constructor’s parameter.
    5)
    This class will have a print function as well that will be called from the
    main using an object of this subclass.
    a.
    You CAN choose to call the print function of the base class
    from
    this function if you like.

    Main Function
    The following explains what you should have in the main function of your
    program:
    1)
    You are going to create a vector of objects from the subclass NOT the
    base class. You can call the vector anything you like.
    2)
    You a
    re going to create five objects of the subclass. You can create
    the objects however you like but remember to pass in a string as the
    only parameter to each object’s constructor.
    a.
    Remember that when you create the objects of the subclass you
    are going to con
    struct the base class too and set the values of
    its four member variables to the values in the text file.
    b.
    Again, how you do that and where is up to you.
    3)
    How you add the objects to the vector is up to you but remember our
    vector example from class.
    4)
    You are
    going to iterate through the vector and call the print function of
    each subclass object, so that it prints the string in the subclass and the
    four member variables in the base class.
    a.
    The format of the output is up to yo

    Exception Handling
    1)
    The
    only exception handling you
    need
    to have in this program deals with
    accessing the file.
    2)
    If you are unable to read the file

    for example, if it doesn’t exist, then you
    should throw and exception and terminate the program.
    3)
    This is obviously going to occur
    when you try to read the file so you have to
    figure out where to handle this exception.
    4)
    This
    WILL be testing for this and your file
    will be deleted
    and try to run your
    program.
    5)
    You do not need to print anything out upon catching the exception, just terminate the program






  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should have done this as soon as the assignment instructions document was handed to you, but perhaps it is not too late: ask your instructor to explain what are the practical implications of "to handle different data type as required", e.g., does this mean that when the instruction says "Could be an int, float, or double", it means that this should be a template parameter so that the programmer using this class template can choose the type (although limited to this set of three), rather than you as the programmer writing the class template?

    I recommend that you do this because other than that part, I see no use for a class template according to the other instructions. So, if it is not that part, then perhaps there was an oversight in the instructions. Alternatively, it could be that your instructor is incompetent and just wants you to have useless template syntax around to demonstrate that you know what template syntax looks like. As much as I would like to believe that your instructor is a good teacher rather than an incompetent one, the part of your instructions that require you to uselessly publicly inherit from a base class that should not be a polymorphic base class (or involve techniques outside of the OOP norm) makes me doubt it.
    Last edited by laserlight; 04-19-2018 at 04:40 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    what do i use try catch throw for the exception handling

  9. #9
    Guest
    Guest
    Something like below.
    Code:
    try {
        // code that can throw
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
    } catch (...) {
        std::cerr << "unknown exception" << std::endl;
    }
    Make sure you solve the other issues first though. If you haven't the slightest idea how to use templates yet your assignment expects it, then you must have not been paying any attention.

    Quote Originally Posted by laserlight
    e.g., does this mean that when the instruction says "Could be an int, float, or double", it means that this should be a template parameter so that the programmer using this class template can choose the type
    That's how I would interpret it. The class members are std::string, T, T, bool.

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    Code:
    try {
            throw = 20;
        }
        catch ()
    sorry how do i fix

  11. #11
    Guest
    Guest
    Ah, troll. GReaper or whoever said it was right. Can't believe people do this as a hobby.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-09-2012, 06:13 PM
  2. Getting my Sega Genesis to display properly
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-10-2007, 01:40 PM
  3. string to bool
    By sql.scripter in forum C++ Programming
    Replies: 6
    Last Post: 05-22-2005, 10:22 AM
  4. Sega naming conspiracy
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-12-2001, 11:16 PM

Tags for this Thread