473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with my program (code inside)

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[i]))

abc[i]=toupper(abc[i]);

}

}

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[i] == b.abc[k])

common_abc[i] = a.abc[i];

}

}

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;

}
Jul 19 '05 #1
1 1903


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
kb******@gascad.at
Jul 19 '05 #2

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

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
13
by: mgorbach | last post by:
Im writing a program that does monte carlo simulation and im having trouble figuring out how to get the threading model right. I have a simulation class which contains all simulation data and...
8
by: SK | last post by:
Hi I am trying to write a simple C program with devc++ as the complier using the concept of arrays. I cannot get the program to compile without mutiple errors. If anyone has the time to help me...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
10
by: wazzup | last post by:
C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far. Create a program to print nested squares Input:...
2
by: pa2ratczi | last post by:
Hi there, am having trouble with my program, its a file locator program just like a search engine in a local drive: heres wat i have coded: Private Sub Command1_Click() Dim SearchFile As String...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.