Help with my program (code inside)

Discussion in 'C++' started by Silver, Nov 14, 2003.

  1. Silver

    Silver Guest

    Hi everyone,

    I'm writing a program that has to do them following

    main() :
    -------
    3 objects are declared (two of type A and one of type B)
    The function frd_fun is called for two objects (B and one of A).

    C frd_fun(A a, B b)
    --------------------
    it returns an object of type C which contains a pointer that points to a
    string. This string contains the common elements of the strings in A and B.

    The description I give is short cause I don't you to write the code for me,
    just point me my mistakes.

    I also would like to have a destructor function for all my classes, where I
    will deallocate the memory (using delete)

    here is my code

    // ÁÓÊÇÓÇ 2

    // ÐÑÏÃÑÁÌÌÁÔÉÓÔÉÊÅÓ ÔÅ×ÍÉÊÅÓ - 7o ÅÎÁÌÇÍÏ 2003-2004

    // ¼íïìá: ÔÆÁÊÁÓ ÁÑÃÕÑÉÏÓ

    // AEM : 4625

    #include <iostream>

    #include <ctype.h>

    using namespace std;

    class B;

    class C;

    class A

    {

    private:

    int n;

    char* abc;

    int dynam; // An einai 0 den exei ginei diniamiki katanomi mnimis

    public:

    A(); // Default constructor definition

    A(int x); // Constructor definition

    ~A();

    //Friend function

    friend void frd_fun(A a, B b);

    };

    A::A()

    {

    dynam = 0;

    cout << endl << "Default constructor A called.";

    n = 10;

    abc = "ABCDEFGHIJ";

    }

    A::A(int x)

    {

    dynam = 1;

    cout << endl << "Constructor A called.";

    n = x;

    abc = new char[n+1];

    cout << endl << "Dwse ena string me " << n << " xaraktires --> ";

    cin >> abc;

    }

    A::~A()

    {

    // if (dynam)

    // delete [] abc;

    cout << endl << " H klasi A katastrafike";

    }

    class B

    {

    private:

    int n;

    char* abc;

    public:

    B(int x); // Constructor

    B(){ cout << endl << "Default constructor B called.";} // Default
    constructor

    ~B(); // Destructor

    //Friend function

    friend void frd_fun(A a, B b);

    };

    B::B(int x)

    {

    cout << endl << "Constructor B called.";

    n = x;

    abc = new char[n+1];

    cout << endl << "Dwse ena string me " << n << " xaraktires --> ";

    cin >> abc;

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

    {

    if (isalpha(abc))

    abc=toupper(abc);

    }

    }

    B::~B()

    {

    cout << endl << " H klasi B katastrafike";

    }

    class C

    {

    private:

    int n;

    char* abc;

    public:

    C(int x); // Constructor definition

    ~C(); // Destructor

    void setAbc(char* txt);

    };

    C::C(int x)

    {

    cout << endl << "Constructor C called.";

    n = x;

    abc = new char[n+1];

    }

    C::~C()

    {

    cout << endl << " H klasi B katastrafike";

    }



    void C::setAbc(char* txt)

    {

    abc = txt;

    }

    void frd_fun(A a, B b)

    {

    C c2(1);

    char common_abc[10];

    for (int i = 0; i < a.n; i++)

    {

    for (int k = 0; k < b.n; k++)

    {

    if (a.abc == b.abc[k])

    common_abc = a.abc;

    }

    }

    c2.setAbc(common_abc);

    cout << common_abc;

    }



    int main()

    {

    int k = 0, l = 0; // ÏÑÉÓÌÁÔÁ Í ÃÉÁ ÔÇÓ ÓÕÍÁÑÔÇÓÅÉÓ ÁÑ×ÉÊÙÍ ÓÕÍÈÇÊÙÍ

    // ÔÙÍ ÊËÁÓÅÙÍ Á ÊÁÉ Â


    cout << endl << "Tha oristoun 2 antikeimena typou A,"

    << endl << " ena gia kathe ekdosi tis sinartisis arxikwn sinthikwn."

    << endl << endl << "Dwse to mikos xaraktirwn tou string --> ";

    cin >> k;


    cout << endl << endl << "Twra tha oristei ena antikeimeno typoy B."

    << endl << "Dwse to mikos xaraktirwn tou string --> ";

    cin >> l;


    A a1;

    A a2(k);

    B b(l);

    if (k > 10)

    frd_fun(a2, b);

    else

    frd_fun(a1, b);





    cout << endl;

    return 0;

    }
     
    Silver, Nov 14, 2003
    #1
    1. Advertising

  2. Silver wrote:
    >
    > Hi everyone,
    >
    > I'm writing a program that has to do them following
    >
    > main() :
    > -------
    > 3 objects are declared (two of type A and one of type B)
    > The function frd_fun is called for two objects (B and one of A).
    >
    > C frd_fun(A a, B b)
    > --------------------
    > it returns an object of type C which contains a pointer that points to a
    > string. This string contains the common elements of the strings in A and B.
    >
    > The description I give is short cause I don't you to write the code for me,
    > just point me my mistakes.
    >
    > I also would like to have a destructor function for all my classes, where I
    > will deallocate the memory (using delete)


    At the moment you use the words:

    dynamic allocation
    destructor
    class

    in one sentence, the next logical question is:
    What about the copy constructor and the assignment operator?

    Scrolling through your source code I see: not implemented.
    So looking further: is it a problem.
    Again scrolling a little bit: Yes, it is.
    frd_fun( A a, B b )
    takes its parameters by value, thus copies of the callers arguments
    are made. Since you didn't provide a copy constructor of your own, the
    compiler provided one which does ... the wrong thing.

    [snip a long posting of unreadable code which lacks indenting and has lots
    of white lines in it]


    If that doesn't answer your question, then I suggest you reread your original
    posting and just using what you have written try to answer: What was the question
    expressed in that posting?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    37
    Views:
    3,838
    Alan Silver
    Feb 10, 2005
  2. keithb
    Replies:
    1
    Views:
    962
    Bruce Barker
    Mar 29, 2006
  3. Peter Nolan
    Replies:
    4
    Views:
    11,060
    E. Robert Tisdale
    Apr 8, 2004
  4. Alexander Mahone

    Running a program (gperf) inside another C program

    Alexander Mahone, May 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    439
    Flash Gordon
    May 26, 2008
  5. Replies:
    3
    Views:
    372
    Pascal J. Bourguignon
    Oct 28, 2012
Loading...

Share This Page