3 Replies - 2293 Views - Last Post: 13 February 2009 - 08:36 PM Rate Topic: -----

#1 agentkirb   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Object oriented programming help

Posted 13 February 2009 - 05:42 PM

So, I'm taking a C++ class at college right now and for the project we are on he wants to stress object oriented programming. Wants us to use constructors, the vector type, all that stuff. Here's what I have so far.

#include <iostream.h>
#include <stdlib.h>
#include <vector>

class PhoneBook
{
private:
Record vector <class> Records; //syntax error before <
public:
void add(Record pN);
bool search(PhoneNumber pN); //'Record' not declared in scope; parse error before )
void list();// 'PhoneNumber' not declared in scope; parse error before )
PhoneBook();//maybe size as argument
};

class PhoneNumber
{
private:
		int areaCode;
		int number;
public:
	   Phonenumber(int aC, int n);
	   int getAreaCode();
	   int getNumber();
	   void setAreaCode(int area);
	   void setNumber(int num);
};

class Record
{
private:
	   int id;
	   PhoneNumber phoneNumber;
public:
	   Record(int i, PhoneNumber pN);
	   int getId();
	   PhoneNumber getPhoneNumber();
	   void setID(int iD);
	   void setPhoneNumber(PhoneNumber pN);
};


This is JUST the declaration of classes. I also put definitions below the main function as a holder so that it doesn't give me an error because I don't have a matching definition.

Anyways, I put the errors in as comments. The idea of the project is I have some kind of "phonebook" where you can add, delete, print the entries. The phonebook is going to be the vector (the vector stores all of the entries). The phonebook class has your main add/delete/print functions. The record class has the id and phone number, as well as funtions to manipulate them. With the Phonenumber class, same story.

The "Record(int i, PhoneNumber pN);" is supposed to be a contructor and there is one for each class. Thats one of the things he wants us to practice with the project, and I'm not completely sure I'm doing it right.

Any help you can give me is appreciated.

Is This A Good Question/Topic? 1
  • +

Replies To: Object oriented programming help

#2 Lillefix   User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Object oriented programming help

Posted 13 February 2009 - 05:54 PM

Record vector <class> Records; //syntax error before <

syntax is:
std::vector <TYPE> NAME;

therefore:
std::vector <Record> records


Also, as your compiler says, you have not declared Record or PhoneNumber when you declare PhoneBook. Should be as simple as to move these declarations before declaring PhoneBook.

This post has been edited by Lillefix: 13 February 2009 - 05:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 agentkirb   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Object oriented programming help

Posted 13 February 2009 - 08:09 PM

View PostLillefix, on 13 Feb, 2009 - 04:54 PM, said:

Record vector <class> Records; //syntax error before <

syntax is:
std::vector <TYPE> NAME;

therefore:
std::vector <Record> records


Also, as your compiler says, you have not declared Record or PhoneNumber when you declare PhoneBook. Should be as simple as to move these declarations before declaring PhoneBook.


Thanks dude. For some reason I thought my "bool search(PhoneNumber pn);" type function declarations were wrong because I was supposed to put an & or some special symbol in front of PhoneNumber like its a pointer or something. The solution ended up being much simpler.

One last related question: For example, in the "search" function I just mentioned. The purpose is that when I'm adding an entry I want to check and make sure I don't already have an entry for that person. I'll call the search function within the add function. But considering one of the arguments is "PhoneNumber pn". Would I call it with something like:

PhoneBook::void add(Record pN)
{
bool value;
//instructions
value = search (a); 
}




Or something to that effect? "a" would be a "PhoneNumber" variable. I suppose "a" would be the phone number that the customer inputs. But thats where it gets confusing... why do I need to put the "PhoneNumber" class before it? I'd assume it has something to do with being able to "access" the private variables from within the PhoneNumber class. But then again, since the search function is in the PhoneBook class, thats a little confusing how that works. Maybe access it through the vector since the vector is in the Phonebook class like this:

if(vectorname[i].phoneNumber.number==a)//does this area code already exist?



I might've just answered my own question in my rambling, but I'm just kind of guessing here so I would like confirmation if I'm right or not.

(by the way, just realized I could've asked the exact same question for the add function too)
Was This Post Helpful? 0
  • +
  • -

#4 agentkirb   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Object oriented programming help

Posted 13 February 2009 - 08:36 PM

Also, I'm getting this weird compiler error. Here is the relevant code:

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook

void PhoneBook::add(Record pN)
{
}//end add
bool PhoneBook::search(PhoneNumber pN)
{
}//end search
void PhoneBook::list()
{
}//end list



PhoneNumber::PhoneNumber(int aC, int n)
{
}//end phonenumber 
int PhoneNumber::getAreaCode()
{
}//end getAC
int PhoneNumber::getNumber()
{
}//end GN
void PhoneNumber::setAreaCode(int area)
{
}//end setAC
void PhoneNumber::setNumber(int num)
{
}//end setN
Record::Record(int i, PhoneNumber pN)
{
}//end record
int Record::getId()
{
}//end getID
PhoneNumber Record::getPhoneNumber()
{
}//end getPN
void Record::setID(int iD)
{
}//end setID
void Record::setPhoneNumber(PhoneNumber pN)
{
}//end setPN



I'm getting a compiler error that says "no matching function for call to 'PhoneNumber::PhoneNumber()'. And it says its at line 95, which is { "start function" symbol right under the Record::Record(int i,PhoneNumber pN) function definition. Thats what is confusing me.

And then it has "suggestions" for what to use. But the confusing thing is I can't find where 'PhoneNumber::PhoneNumber()' is to change it.

The code I posted is JUST the empty function definitions to all of the functions I defined in my classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1