Need some explain with this problem

Hello There, Can I someone explain this problem. I dont understand what mean. I have read over and over i still dont get it.

Assume you need an application that operates on two kinds of data types D1 and D2. Both D1 and D2 need to be defined with typedef.

Assume you need some functions to operate on the data: f1D1() and f2D1() operating on D1 and f1D2() operating on D2.

Assume a helper function fhD1() and helper integer intD1 to help the functions operating on D1 (not available to others).

Assume global data g1 and g2 of some built in type.

Assume the main created a variable of type D1 and then operates on it. D2 is not needed in main, but it is needed in f1D2().

Given this, and disregarding other details, design all files needed. Use inclusion-prevention. For each file you have, show important elements, disregarding other details. Submit as actual files with the actual names.

Here is what I have try

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    #typedef D1
    #typedef D2
   
 Main.cpp
    void main 
    {
        D1 (a);
    }
  
  D1.cpp
    void D1 (a)
    {
        f1D1 (c);
        f1D2(b);
    }
  
  D2.c
    void D2(z)
    {
        fiD2 (x);
    }
   
 fhD1.cpp 
    viod fhD1 (p)
    {
        int D1 (p);
    }
Last edited on
Just telling inclusion-prevention , means using macros to prevent the compiler to compile multiple times a class , so you need to create class(es) or struct . g1 and g2 are instance of that class or struct . intD1 is a data member , fhD1 is a method member. Because you say that it must be defined with typedef I think you should create a struct .

In D1.h :
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef STRUCT_D1
#define STRUCT_D1

typedef struct{
	int intD1;
	void fhD1(void)
	{
		std::cout << intD1 << std::endl;
	}
}D1;

#endif 


in D2.h :
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef STRUCT_D2
#define STRUCT_D2

typedef struct{
	int intD2;
	void fhD2(void)
	{
		std::cout << intD2 << std::endl;
	}
}D2;

#endif 


in main.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

#include "D1.h"
#include "D2.h"

using namespace std;

D1 g1;
D2 g2;

int main(int argc , char * argv[])
{
	g1.intD1 = 1;
	g1.fhD1();

	g2.intD2 = 2;
	g2.fhD2();

	system("pause");

	return EXIT_SUCCESS;
}


that's what I came up with from what I understood from this question.
Give a try.
Does Dfh1 and intD1 functions on D1?

Assume a helper function fhD1() and helper integer intD1 to help the functions operating on D1 (not available to others).

and also do we have to create a class or function for D1 or D2?

I still dont understand. why you use struct? Do we support to use fuction?
Last edited on
typedef was used to declared a new type like a struct in C, now typedef is used to create an alias like so : typedef unsigned int uint; uint a = 1;

We can create classes instead . But the question is still blurry to me , essentialy

Assume a helper function fhD1() and helper integer intD1 to help the functions operating on D1 (not available to others).


It would help if I know the context of it , where have you seen this , etc.
I wish he give us more but that all he give to us
In a class scope , functions are called methods , variable are called attributes.
methods does not operate on class , they operate on instance . A class is a definition.

I did not find a definition for an helper function , but the part
(not available to others).

could means encapsulation , major concept of OOP , so in that case probably we should use a class instead of a struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class D1
{
private:
  int data = i;
  void privateMethod1(int i)
  {
     data = i;
  }
public:
 void fhD1(int i )
 {
    privateMethod1(i);
 }
};

D1 g1;

int main()
{
   int intD1 = 3;
   g1.fhD1(intD1);
  return 0;
}
But isnt class only for java? If yes, it wont work because my teacher say c or cpp only. I was think like you too but i dont know if class also can use by c or cpp
Last edited on
class is for oriented-object programming language which includes c++ and many more.

c use struct not class.
but c++ can use struct and class.
Topic archived. No new replies allowed.