printing to screen from a vector

This is a discussion on printing to screen from a vector within the C++ Programming forums, part of the General Programming Boards category; I want to make a vector that can input data to via classes and save it in a file. I ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    printing to screen from a vector

    I want to make a vector that can input data to via classes and save it in a file.

    I have done some testing but I don't see how I do cout the entries?
    I am assuming I have a vector here?

    Code:
    ParentClass people[ ] = {
    			SubClass("Name", "Adress", Phone)
    	};
    I have made all the appropriate classes and and when running, I get this output:
    Code:
    ParentClass constructor...
    SubClass constructor...
    SubClass destructor...
    ParentClass destructor...
    0x22fef0ParentClass destructor...
    the last one is a little weird... did I forget to delete something?

    I tried this, but it doesn't print extra anything on screen:
    Code:
    std::cout << people;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,748
    One problem is that people is an array of ParentClass objects. You created a SubClass object, and SubClass is presumably derived publicly from ParentClass, but then type slicing happens: people[0] is a ParentClass object, not a SubClass object, but with parts that are a copy of the ParentClass subobject of the SubClass temporary object.

    A solution here is to store an array of (smart) pointers to ParentClass. These pointers can thus point to SubClass objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Yes, SubClass is derived from ParentClass.

    the "string Name" and "string Adress" are in the ParentClass, while "int Phone" is in the SubClass.

    How would I use pointers to point directly to an object in the class? and should I make another array to save those pointers in?

    In the final programme, I will be writing the arrays to a file and save that file and not to the screen (just mentioning the full scenario), but I assume the process is pretty much the same.

    The full code is as follows:
    ParentClass.h:
    Code:
    class ParentClass
    protected:
    	ParentClass();														// constructor
    	ParentClass(string Name, string Adress);	// overloaded constructor!
    	virtual string GetName() const { return itsName; }		// inline constant funktion
    	virtual void SaveName(string Name) { itsName = Name; }		// inline function
    	virtual string GetAdress() const {return itsAdress; }		// inline constantfunction
    	virtual void SaveAdress(string Adress) { itsAdress = Adress; }	// inline function
    private:
                string itsName;
                string itsAdress;
    
    // overloaded ParentClass
    ParentClass::ParentClass(string Name, string Adress):			         itsName(Name), itsAdress(Adress)
    {
    	cout << "ParentClass constructor...\n";
    }
    SubClass.h:
    Code:
    // inheritance from ParentClass
    class SubClass: virtual public ParentClass	{
    public:
    	SubClass();
    	SubClass(string Name, string Adress, int Phone); // overloaded constructor
    	virtual ~SubClass() { std::cout << "SubClass destructor...\n"; }	// destructor!
    	virtual string GetPhone() const { return itsPhone; }
    private:
    	int itsPhone;
    };
    // overloaded SubClass
    
    SubClass::SubClass(string Name, string Adress, int Phone):
    		    ParentClass(Name, Adress),
    		    itsPhone(Phone)
    {
    	cout << "SubClass constructor...\n";
    }
    and finally main.ccp:
    Code:
    #include <iostream>
    #include <string>
    #include "ParentClass.h"
    #include "SubClass.h"
    
    using std::string;
    
    int main() {
    	// Array of objects to store in file.
    ParentClass people[ ] = {
    			SubClass("Name", "Adress", Phone)
    	};
    
    std::cout << people;
    (I've changed the names of the classes to English, so it's easier to read)m so if there is some syntaxes in names, don't mind it, it's not there in my programme)
    Last edited by Roffemuffe; 04-24-2011 at 09:48 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    This is the solution I've come up with so far:
    Code:
    SubClass *pSubs = new SubClass("Billy", "Home", 555);
    
    	string name = pSubs->GetName();
    	string adress = pStud->GetAdress();
    	string phone = pStud->GetPhone();
    	std::cout << name << ", " << adress << ", " << phone << std::endl;
    
    	delete pSubs;
    output:
    Code:
    ParentClass constructor...
    SubClass constructor...
    Billy, Home, 555
    SubClass destructor...
    ParentClass destructor...
    I got rid of that weird extra destructor too.

    This seems like a fine solution for one entry, but I have no idea where to begin with multiple entries?
    And how do I move this pointer into a vector? or should I use an array instead?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    std::vector<std::shared_ptr<ParentClass>> v;
    v.push_back(std::make_shared<SubClass>("Billy", "Home", 555));
    //...

    This assumes you use C++0x. Otherwise this won't work.
    std::shared_ptr is a smart pointer. It will automatically delete the memory for you.
    std::make_shared creates a shared pointer with an appropriate object. It is equivalent with:

    std::shared_ptr<SubClass>(new SubClass("Billy", "Home", 555));
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is this not printing to screen?
    By lilbo4231 in forum C Programming
    Replies: 2
    Last Post: 03-07-2011, 02:10 PM
  2. printing in screen
    By imagetvr in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2008, 10:35 AM
  3. Printing \n on the screen in C
    By swgh in forum C Programming
    Replies: 4
    Last Post: 05-18-2007, 03:52 PM
  4. printing on screen question
    By tsubasa in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2006, 02:36 AM
  5. printing to screen fast
    By h_howee in forum C++ Programming
    Replies: 7
    Last Post: 03-06-2006, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21