LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2005, 07:42 AM   #1
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 170

Rep: Reputation: 15
classes in C++


Hello,

I'm learning classes right now and I'm trying to read in a file that contains two strings, one has the name of a pet and the second contains what type of a pet is it. All of the pets should be read into an array of Pet objects, but I don't know how I should read the contents in, is it something like:

void fillArray(Cat*& catArray, int& size){
string n, t;
indexcout(0);

while(ifs << n << t){

if(indexcout == size)
expandArray(catArray, size);

catArray[indexcout].setName(n);
catArray[indexcout].setType(t);
}

I'm using a dynamic array of 'Cat', inside the cat I have the private string variables that hold the name and type of the pet...then I get the accessors and setters....which I'm trying to use to read into....I don't know if this is the correct way of doing it...

My next problem is accessing the contents once they are read...I want to be able to sort the array in ascending/descending order...how would I go about doing that?

Thank you..I'm new to C++, especially classes...I think vectors are the easiest and am able to do this using vectors but I want to know how to use classes...any help would be appreciated...thank you again
 
Old 07-05-2005, 08:51 AM   #2
puishor
Member
 
Registered: Jun 2005
Location: Bucharest
Distribution: Debian,Knoppix,Slackware
Posts: 91

Rep: Reputation: 15
Don't really understood what is your problem , but I hope this would help.
If this is not what you seek for , post a reply , I would try to help you.

Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>

class Cat
{
   public:

   void SetName( char p[255]  )
   {
     strcpy( pet , p  );
   }

   void SetType( int n  )
   {
     type = n;
   }

   char* GetName()
   {
    return pet;
   }

   int GetType()
   {
    return type;
   }

   private:

    char pet[255];
    int type ;
};



Cat catArray[100];


int Read()
{
    //////////////////// Here read from file , formatted data , line by line.

  ifstream   f( "test.txt"  );
  char buffer[255]  ;
  int n = 0 ;
  char pet[255];
  int type ;


  while(! f.eof()  )
  {
      f.getline( buffer , 255  );
      sscanf( buffer , "%s %d" , pet , &type  );
      catArray[n].SetName( pet  );
      catArray[n++].SetType( type ) ;

  }
  ;

  return n;

}


void Display( int n  )
{
  for( int i =0  ; i < n; i++  )
  {
    cout << catArray[i].GetName() <<" - "<< catArray[i].GetType() <<  endl ;
  }
}

int main()
{
  Display( Read() ) ;
  return 0;
}
 
Old 07-05-2005, 07:05 PM   #3
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 170

Original Poster
Rep: Reputation: 15
Thank for the reply...had an issue but just resolved it..sorry for the post

Last edited by niteshadw; 07-05-2005 at 07:12 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
C++ nested classes enemorales Programming 5 05-23-2005 03:40 PM
RHCE Classes ninjaz Linux - Certification 3 05-03-2005 04:23 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration