help with classes

This is a discussion on help with classes within the C++ Programming forums, part of the General Programming Boards category; here is my program, i am trying to set s3 equal to s1.unionSet(s2), unionSet works fine when i dont set ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    help with classes

    here is my program, i am trying to set s3 equal to s1.unionSet(s2), unionSet works fine when i dont set it to s3, but i need to set it equal to s3 for a school project. someone please help me.

    //header

    #include <fstream.h>
    #include <iostream.h>
    #ifndef header_h
    #define header_h

    class PrimeNumSet
    {
    public:
    PrimeNumSet(const char obj_name[] = 0, int num1 = 0, int num2 = 0, int num3 = 0, int num4 =0, int num5 =0, int num6 = 0);
    void emptySet();
    void set_obj(const char obj_name[], int num1 = 0, int num2 = 0, int num3 = 0, int num4 =0, int num5 =0, int num6 = 0);
    void inputSet();
    void printSet();
    void insertElem(int e);
    void unionSet(const PrimeNumSet &s2 );
    void interSect(const PrimeNumSet &s2 );
    void deleteElem(int e);
    int isEqualTo(const PrimeNumSet &s2 );

    private:
    int set[100];
    int input[100];
    int valid(int e) const;
    char name[31];

    };

    #endif

    //funcitons


    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <math.h>
    #include "primeSet.h"
    #include <string.h>


    PrimeNumSet::PrimeNumSet(const char obj_name[], int num1, int num2, int num3, int num4, int num5, int num6)
    {

    set_obj(obj_name, num1, num2, num3, num4, num5, num6);
    strcpy(name, obj_name);

    }
    void PrimeNumSet::emptySet()
    {
    for(int i = 0; i <102; i++)
    {
    set[i] = 0;
    }
    }
    void PrimeNumSet::set_obj(const char obj_name[], int num1, int num2, int num3, int num4, int num5, int num6)
    {
    emptySet();
    if (valid(num1))
    {
    insertElem(num1);
    }
    if (valid(num2))
    {
    insertElem(num2);
    }
    if (valid(num3))
    {
    insertElem(num3);
    }
    if (valid(num4))
    {
    insertElem(num4);
    }
    if (valid(num5))
    {
    insertElem(num5);
    }
    if (valid(num6))
    {
    insertElem(num6);
    }
    }

    void PrimeNumSet::inputSet()
    {
    int e;
    cin >> e;
    while(e != 999)
    {
    insertElem(e);
    cin >> e;
    }
    out << "\n Entry complete; inputSet() called."<<endl;
    }

    void PrimeNumSet:rintSet()
    {
    int i =0;
    out << " The set " << name << " is:"<< endl;
    out << "{ ";
    for(i=0; i < 102; i++)
    {
    if(set[i] == 1)
    {
    out << i << " ";
    }
    }
    out << "}"<< endl;
    out << " printSet() called" << endl;
    }

    void PrimeNumSet::insertElem(int e)
    {
    if(valid(e))
    {
    set[e] = 1;
    }
    }
    int PrimeNumSet::valid(int e) const
    {
    int validSet[101] = {0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0};

    return validSet[e] == 1;
    }

    void PrimeNumSet::unionSet(const PrimeNumSet &s2)
    {
    for(int i = 0; i <102; i++)
    {
    set[i] = (set[i] | s2.set[i]);
    }
    out << "\n unionSet() called." << endl;
    }


    //main

    #include <stdlib.h>
    #include <fstream.h>
    #include <iomanip.h>
    ofstream out("cphw2e.txt");
    ifstream fcin("cphw2e_i.txt");
    #include "primeSet.h"
    #include "primeSettest.cpp"
    // Note : in all files replace all cout by out
    // and replace all cin by fcin

    int main()
    {
    PrimeNumSet s1("s1"), s2("s2"), s3("s3"), s4("s4");
    PrimeNumSet s5("s5", 10, 21, 67, 59, 26, 41), s6("s6", 9, 71, 97, 21);

    s1.inputSet();
    s1.printSet();
    s2.inputSet();
    s2.printSet();
    s3 = s1.unionSet(s2);/*******this line wont work like this **/
    out << endl << " The union of s1 and s2 is: " << endl;
    s3.printSet();
    return 0;
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    s3 = s1.unionSet(s2);/*******this line wont work like this **/
    PrimeNumSet::unionSet() doesn't return a value so you can't assign the result of it to anything.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21