Prime class

Z

zacariaz

Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.

@code start:
#include <iostream>
int main() {

Prime P;

for (int n = 0; n < 8; n++)
P.next();

for (int n = 0; P.get(n) != 0;n++)
std::cout << P.get(n) << std::endl;

std::cout << "Error, not enough primes!";
std::cin.get();
}
@code end!

The most important function is of course that which calculates the
primes: Prime::next() and it calculates one prime per execution. (is
that the right expression?)
Then there is the Prime::get() which enables you to get a specific
prime.
Other functions will come, but thats not important right now.


Well, i wont say much more at the time being, but i hope to hear some
advices, oppinions, etc.


Regards
 
S

Salt_Peter

Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)
~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);


Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_error
}
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.


Why not use a functor?
 
Z

zacariaz

Salt_Peter skrev:
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)
~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);


Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_error
}

Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.


Why not use a functor?

Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.
 
Z

zacariaz

Salt_Peter skrev:
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)
~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);


Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_error
}

Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.


Why not use a functor?

Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.
 
S

Salt_Peter

Salt_Peter skrev:
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)
~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);


Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_error
}

Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.


What i suggested is nothing new, C++ constructor's supports an
initialization list. Why allocate a member and then initialize it to
some value when you can do both simultaneously.

class A {
int a;
public:
A() : a(0) { } // sets private member
A(int n) : a(n) { }
void foo() const { }
};

std::size_t is a default size type returned by sizeof() operator -
which is implementation defined. In the above example, foo() is
declared const because i chose not to let foo() access the object's
internals. That way foo() does not get passed the hidden 'this'
parameter. So in your case, declaring get() as constant guarentees that
get() will not attempt to modify your private parts - even by accident.
This is an important concept since it improves the sfaety of the class
and prevents side-effects.

To understand vector's at() function, uncomment the call to at() below
since the vector does not have an 11th element.

#include <iostream>
#include <ostream>
#include <vector>
#include <stdexcept>

int main()
{
try {
std::vector< unsigned > vu(10, 99);
for(std::size_t i = 0; i < 10; ++i)
{
std::cout << "vu[" << i << "] = ";
std::cout << vu.at(i) << std::endl;
}
// std::cout << vu.at(10); // will throw std::range_error
} catch (const std::exception& r_e)
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
return 0;
}

/*
vu[0] = 99
vu[1] = 99
vu[2] = 99
vu[3] = 99
vu[4] = 99
vu[5] = 99
vu[6] = 99
vu[7] = 99
vu[8] = 99
vu[9] = 99
*/
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.


Why not use a functor?

Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.


Don't worry about functors for now. You might consider them later,
though. Something like returning the set of even, odd or prime numbers
are perfect candidates for functors.
 
G

Gary Wessle

Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);

why return(0)?
how about
return 0;
the same for below.
 
Z

zacariaz

Salt_Peter skrev:
Salt_Peter skrev:
(e-mail address removed) wrote:
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.

I have made a working primeclass, it isnt finished, but it should show
if im making any major mistakes and errors.

@code start:
#include <vector>

class Prime {

std::vector<unsigned int>p;

public:

Prime() {
p.push_back(3);
p.push_back(3);
}

// use an init list
Prime() : p(2, 3) { } // (size, initial value)


~Prime() {}

unsigned int get(int i) {
if (i >= p.size())
return(0);
else if(i == 0)
return(2);
else
return(p);

Since the std::vector already has a bounds_checking accessor at():

unsigned get(size_t idx) const
{
return p.at(idx); // may throw std::range_error
}

Even though i have played with C++ for quite a while, im not very
skilled. There is allways other way to do it. What you wrote above is
very confusing to me, most of the code i have never seens it before,
however if it does the job better im very interested in knowing how it
actually works.


What i suggested is nothing new, C++ constructor's supports an
initialization list. Why allocate a member and then initialize it to
some value when you can do both simultaneously.

class A {
int a;
public:
A() : a(0) { } // sets private member
A(int n) : a(n) { }
void foo() const { }
};

std::size_t is a default size type returned by sizeof() operator -
which is implementation defined. In the above example, foo() is
declared const because i chose not to let foo() access the object's
internals. That way foo() does not get passed the hidden 'this'
parameter. So in your case, declaring get() as constant guarentees that
get() will not attempt to modify your private parts - even by accident.
This is an important concept since it improves the sfaety of the class
and prevents side-effects.

To understand vector's at() function, uncomment the call to at() below
since the vector does not have an 11th element.

#include <iostream>
#include <ostream>
#include <vector>
#include <stdexcept>

int main()
{
try {
std::vector< unsigned > vu(10, 99);
for(std::size_t i = 0; i < 10; ++i)
{
std::cout << "vu[" << i << "] = ";
std::cout << vu.at(i) << std::endl;
}
// std::cout << vu.at(10); // will throw std::range_error
} catch (const std::exception& r_e)
{
std::cerr << "Error: " << r_e.what();
std::cerr << std::endl;
}
return 0;
}

/*
vu[0] = 99
vu[1] = 99
vu[2] = 99
vu[3] = 99
vu[4] = 99
vu[5] = 99
vu[6] = 99
vu[7] = 99
vu[8] = 99
vu[9] = 99
*/
}

unsigned int next() {

p.front() += 2;

for (int i = 1; p.front() >= p * p; i++)
if (p.front() % p == 0) {
p.front() += 2;
i = 0;
}

p.push_back(p[0]);
return(p.back());
}

};
@code end!

For those who cant figure out how it work, here is a little piece of
code that displays the first ten primes.

Why not use a functor?

Again, all those weird expressions ;-) what is a funtor, do you mean
moving the functions outside the class? if thats the case, i simple
like it this way and the end result should be the same, if im not
mistaken.


Don't worry about functors for now. You might consider them later,
though. Something like returning the set of even, odd or prime numbers
are perfect candidates for functors.

As you must have used quite alot of time explaining this i can only
oppologize that i understand close to nothing of it. Maybe its because
english is not my first language or maybe im just plain stupid, either
way i wont ask you to explain again.
Thank you for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Q for a source code in an exercise 1
Prime number generator 11
Crossword 2
Lexical Analysis on C++ 1
Python prime numbers 4
Prime Numbers 19
Prime Factors 7
Dynamic programming 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top