Kezdőoldal » Számítástechnika » Programozás » C++ nyelvben jártas személy...

C++ nyelvben jártas személy segítene? Van egy cpp és egy hozzá tartozó header fájlom. A program tökéletesen működik, és lefut, ezzel nincs gond. Viszont értelmeznem is tudni kellene, nagyjából sorról sorra. Na ez viszont nem igazán megy.

Figyelt kérdés

A main.cpp:


#include "autolist.h"

#include <iostream>

#include <set>

#include <algorithm>

#include "autolist.h"

#include <vector>


class CntInteger

{

static int obj;

int value;

public:

CntInteger( int v ): value( v )

{

++obj;

}


CntInteger( const CntInteger& rhs ): value( rhs.value )

{

++obj;

}


~CntInteger()

{

--obj;

}


int val() const

{

return value;

}


static int cnt()

{

return obj;

}

};


int CntInteger::obj = 0;


const int max = 1000;


int main()

{

int yourMark = 1;


auto_list<int> li;

li.push_back( new int( 3 ) );

li.push_front( new int ( 4 ) );

if ( 4 == li.front() )

{

auto_list<CntInteger> lc;

for( int i = 0; i < max; ++i )

{

lc.push_back( new CntInteger( i ) );

}

}

if ( 0 == CntInteger::cnt() )

{

yourMark = li.size();

}


std::cout << "Your mark is " << yourMark;

std::endl( std::cout );

}


A hozzá tartozó autolist.h header fájl:


#ifndef AUTOLIST_H

#define AUTOLIST_H

#include<list>

#include<iostream>


template<class T>

class auto_list

{

private:

std::list<T*>* lista;


public:

auto_list();

~auto_list();

void push_back(T* uj);

void push_front (T* uj);

T front();

int size();

};



template<class T>

auto_list<T>::auto_list()

{

lista = new std::list<T*>();

}


template<class T>

auto_list<T>::~auto_list()

{

while(!(lista->empty()))

{

delete lista->front();

lista->pop_front();

}

delete lista;

}


template<class T>

void auto_list<T>::push_back(T* uj)

{

lista->push_back(uj);

}


template<class T>

void auto_list<T>::push_front(T* uj)

{

lista->push_front(uj);

}


template<class T>

T auto_list<T>::front()

{

return *(lista->front());

}


template<class T>

int auto_list<T>::size()

{

return lista->size();

}


#endif


2012. jan. 15. 17:20
 1/1 anonim ***** válasza:

//Kommentekkel írom vissza:

#include "autolist.h"

#include <iostream> //írás-olvasáshoz kell

#include <set> //halmaz adatszerkezet

#include <algorithm>

#include "autolist.h"

#include <vector> //vektor adatszerkezet


class CntInteger

{

static int obj;

int value;

public: //ez a konstruktor

CntInteger( int v ): value( v )

{

++obj;

}

//Másoló konstruktor

CntInteger( const CntInteger& rhs ): value( rhs.value )

{

++obj;

}

//Destruktor

~CntInteger()

{

--obj;

}

//Függvények az adattagok lekérdezéséhez

int val() const

{

return value;

}


static int cnt()

{

return obj;

}

};


int CntInteger::obj = 0;


const int max = 1000;


int main()

{

int yourMark = 1;


auto_list<int> li; //li lista

li.push_back( new int( 3 ) ); // a lista végére 3-at szúr be

li.push_front( new int ( 4 ) ); //a lista elejére 4 kerül

if ( 4 == li.front() )

{

auto_list<CntInteger> lc; //lc egész párok listája

for( int i = 0; i < max; ++i )

{

lc.push_back( new CntInteger( i ) ); //sorra betesszük az egészeket

}

}

if ( 0 == CntInteger::cnt() )

{

yourMark = li.size(); //a minket érdeklő szám a li lista mérete

}


std::cout << "Your mark is " << yourMark;

std::endl( std::cout );

}


//A hozzá tartozó autolist.h header fájl:


#ifndef AUTOLIST_H

#define AUTOLIST_H //include őr

#include<list>

#include<iostream>


template<class T> //sablon

class auto_list //lista osztály

{

private:

std::list<T*>* lista; //T típusú elemeket tartalmazó lista


public:

auto_list(); //konstruktor

~auto_list(); //destruktor

void push_back(T* uj); //beszúrás a végére

void push_front (T* uj); //beszúrás az elejére

T front(); //az első elem T típusú

int size(); //méret lekérdezése

};



template<class T>

auto_list<T>::auto_list()

{

lista = new std::list<T*>();

}


template<class T>

auto_list<T>::~auto_list()

{

while(!(lista->empty()))

{

delete lista->front();

lista->pop_front();

}

delete lista;

}


template<class T>

void auto_list<T>::push_back(T* uj)

{

lista->push_back(uj);

}


template<class T>

void auto_list<T>::push_front(T* uj)

{

lista->push_front(uj);

}


template<class T>

T auto_list<T>::front()

{

return *(lista->front());

}


template<class T>

int auto_list<T>::size()

{

return lista->size();

}


#endif

2012. jan. 15. 19:14
Hasznos számodra ez a válasz?

Kapcsolódó kérdések:





Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu

A weboldalon megjelenő anyagok nem minősülnek szerkesztői tartalomnak, előzetes ellenőrzésen nem esnek át, az üzemeltető véleményét nem tükrözik.
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!