473,466 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

object programming Class

Hi everyone,

I am trying to Print (display) all the items of a vector (class TvectorStat)
which is into another class (class Tpile). Apparently I can't reach m_size
of Class TvectorStat because it is in the private part of the class but I
don't understand why because this class (TvectorStat) belongs to the class
Tpile...Can someone look at my files?

_________________________main.cpp_______
#include "vectorStat.h"
#include "pile.h"

int main()
{

Tpile p;

p.push(11);
p.push(22);
p.push(33);
p.push(44);
p.push(55);
p.push(66);
p.push(77);
p.push(88);
p.push(99);
p.push(111);
cout<<p.printLn(0)<<endl;
cout<<p.printLn(1)<<endl;
cout<<p.printLn(2)<<endl;
cout<<p.printLn(3)<<endl;
cout<<p.printLn(4)<<endl;
cout<<p.printLn(5)<<endl;
cout<<p.printLn(6)<<endl;
cout<<p.printLn(7)<<endl;
cout<<p.printLn(8)<<endl;
cout<<p.printLn(9)<<endl;
cout<<p.printLn(10)<<endl;

p.affiche();

return 0;

}
_____________________________________

________________________pile.cpp___________
// pile.cpp

#include "vectorStat.h"
#include "pile.h"

void Tpile::push(int val)
{
m_data.push_back(val);
}

void Tpile::pop()
{
m_data.pop_back();
}

int Tpile::top()
{
return m_data.back();// int& TvectorStat::back() OU BIEN int
TvectorStat::back()const ?
}

int Tpile::printLn(int pos)
{
return m_data.at(pos);
}

void Tpile::affiche()
{
int i=0;

for (i=0;i<m_size;i++)

cout<<m_data[i]<<endl;
}

___________________________________
_________________pile.h__________________

// pile.h

#ifndef PILE_H
#define PILE_H

#include <iostream>

using namespace std;

class Tpile{

public:

Tpile():m_data(100){}

void push(int val);

void pop();

int top();

int printLn(int pos);

void affiche();
private:

TvectorStat m_data;

};

#endif

______________________________________

__________________________vector.cpp_______
//---------------------------------------------------------
// Fichier : vectorStat.cpp
// esnig - Laboratoire de programmation
//---------------------------------------------------------

#include "vectorStat.h"

void fatalError( const char* str )
{
cout << "TvectorStat Error : " << str << endl ;
exit(1) ;
}

TvectorStat::TvectorStat( int capacity )
:m_capacity(capacity),m_size(0)
{
m_values = new int[m_capacity] ;
if( m_values==NULL )
fatalError("Allocation à la construction") ;
}

TvectorStat::TvectorStat( const TvectorStat& vec )
:m_capacity(vec.m_capacity),m_size(vec.m_size)
{
m_values = new int[vec.m_capacity] ;
if( m_values==NULL )
fatalError("Allocation à la copie") ;
for( int i=0; i<vec.m_size;i++ )
m_values[i] = vec.m_values[i] ;
}

TvectorStat::~TvectorStat()
{
delete[] m_values ;
}

TvectorStat& TvectorStat::operator=( const TvectorStat& opG )
{
if( this != &opG ){
delete[] m_values ;
m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = new int[opG.m_capacity] ;
if( m_values==NULL )
fatalError("Allocation à l'affectation") ;
for( int i=0; i<opG.m_size;i++ )
m_values[i] = opG.m_values[i] ;
}
return *this ;
}

void TvectorStat::push_back( int value )
{
if( full() )
fatalError("push_back sur conteneur plein") ;
m_values[m_size] = value ;
m_size++ ;
}

void TvectorStat::pop_back()
{
if( !empty() )
m_size-- ;
}

int& TvectorStat::back()
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}

int TvectorStat::back()const
{
if( empty() )
fatalError("back sur conteneur vide") ;
return m_values[m_size-1] ;
}

int& TvectorStat::front()
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}

int TvectorStat::front()const
{
if( empty() )
fatalError("front sur conteneur vide") ;
return m_values[0] ;
}

void TvectorStat::insert( int index, int value )
{
if( full() )
fatalError("insert sur conteneur plein") ;

if( index<0 || index>m_size )
fatalError("insert avec index non-valide") ;

for( int i=m_size; i>=index; i-- )
m_values[i+1] = m_values[i] ;
m_values[index] = value ;
m_size++ ;
}

void TvectorStat::remove( int index )
{
if( !validIndex(index) )
fatalError("remove avec index non-valide") ;

for( int i=index; i<m_size-1; i++ )
m_values[i] = m_values[i+1] ;
m_size-- ;
}

int& TvectorStat::at( int index )
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}

int TvectorStat::at( int index )const
{
if( !validIndex(index) )
fatalError("at avec index non-valide") ;
return m_values[index] ;
}
int& TvectorStat::operator[]( int index )
{
return m_values[index] ;
}

int TvectorStat::operator[]( int index )const
{
return m_values[index] ;
}

bool TvectorStat::operator==( const TvectorStat& opG )
{
bool egal = m_size==opG.m_size ;
int i = 0;

while( i<m_size && egal ){
egal = m_values[i] == opG.m_values[i] ;
i++ ;
}
return egal ;
}

bool TvectorStat::operator!=( const TvectorStat& opG )
{
return !(*this==opG) ;
}

bool TvectorStat::empty()const
{
return m_size<=0 ;
}

bool TvectorStat::full()const
{
return m_size>=m_capacity ;
}

int TvectorStat::size()const
{
return m_size ;
}

int TvectorStat::capacity()const
{
return m_capacity ;
}

void TvectorStat::reserve( int newCapacity )
{
int* buffer ;

if( newCapacity<0 )
newCapacity = 0 ;

if( m_size>newCapacity )
m_size = newCapacity ;

m_capacity = newCapacity ;

buffer = new int[newCapacity] ;
for( int i=0; i<m_size; i++ )
buffer[i] = m_values[i] ;

delete[] m_values ;
m_values = buffer ;
}

void TvectorStat::resize( int newSize, int def )
{
if( newSize<0 )
newSize = 0 ;

if( newSize<=m_size ){
m_size=newSize ;
return ;
}

if( newSize>m_capacity )
fatalError("resize supérieur à capcity") ;

while( m_size<newSize )
push_back(def) ;
}

void TvectorStat::swap( TvectorStat& opG )
{
int t_capacity = m_capacity ;
int t_size = m_size ;
int* t_values = m_values ;

m_capacity = opG.m_capacity ;
m_size = opG.m_size ;
m_values = opG.m_values ;

opG.m_capacity = t_capacity ;
opG.m_size = t_size ;
opG.m_values = t_values ;
}

bool TvectorStat::validIndex( int index )const
{
return index>=0 && index<m_size ;
}

ostream& operator<<( ostream& out, const TvectorStat& opG )
{
out << '{' << opG.size() << '/' << opG.capacity() << '}' ;
out << '[' ;
if( opG.empty() )
out << "vide" ;
else{
for( int i=0; i<opG.size()-1; i++ )
out << opG[i] << ';' ;
out << opG[opG.size()-1] ;
}
out << ']' ;
return out ;
}
_______________________________________

_________________________vectorStat.h______
//---------------------------------------------------------
// Fichier : vectorStat.h
// esnig - Laboratoire de programmation
//---------------------------------------------------------

#ifndef VECTORSTAT_H
#define VECTORSTAT_H

#include <iostream>
using namespace std ;

class TvectorStat{
public:
TvectorStat( int capacity ) ;
TvectorStat( const TvectorStat& vec ) ;
~TvectorStat() ;

TvectorStat& operator=( const TvectorStat& opG ) ;

void push_back( int value ) ;
void pop_back() ;
int& back() ;
int back()const ;
int& front() ;
int front()const ;

void insert( int index, int value ) ;
void remove( int index ) ;
int& at( int index ) ; // pourquoi int&

int at( int index )const ;

int& operator[]( int index ) ; // c'est quel opérateur ?
int operator[]( int index )const ;

bool operator==( const TvectorStat& opG ) ;
bool operator!=( const TvectorStat& opG ) ;

bool empty()const ;
bool full()const ;

int size()const ;
int capacity()const ;

void reserve( int newCapacity ) ;
void resize( int newSize, int def=0 ) ;
void swap( TvectorStat& opG ) ;
private:

bool validIndex( int index )const ; // pourquoi on met cette fonction dans
private?
int* m_values ;
int m_capacity ;
int m_size ;
} ;

ostream& operator<<( ostream& out, const TvectorStat& opG ) ;// pourquoi
cette fonction est dehors?

#endif

Jul 22 '05 #1
1 1332

"Stephane Vollet" <st*************@bluewin.ch> wrote in message
news:41**********@news.bluewin.ch...
Hi everyone,

I am trying to Print (display) all the items of a vector (class TvectorStat) which is into another class (class Tpile). Apparently I can't reach m_size
of Class TvectorStat because it is in the private part of the class but I
don't understand why because this class (TvectorStat) belongs to the class
Tpile...Can someone look at my files?


No need to look. Private means private. The fact that
an object is contained within another does not grant
any special access privileges to the containing class.
You'll need to provide for that access specifically, e.g.
via public interface, friends, or public or protected
inheritance.

-Mike
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: Craig Rodrigues | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.object.corba.tao This is a formal Request For Discussion (RFD) to create comp.object.corba.tao as an unmoderated world-wide Usenet newsgroup...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
4
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
62
by: Laurent Deniau | last post by:
I just put the draft of my paper on the web: http://cern.ch/laurent.deniau/html/cos-oopsla07-draft.pdf I would be interested by any feedback from C programmers (with little OO knowledge) to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.