conversion C to CPP prototype and ftn header

I have written a small application in C. I have a header (app.h) in which I declared a structure, and I include the function prototypes. In the app.c file I have several functions that manipulate that structure.

I want to convert the code to CPP. In the c source I think that the static functions that should become private, and other functions should be public.

C EXAMPLE
< static int function1(structure s, int y){
int y1; .....
return y1;
}>

I have other functions that are not static. Some return a value, some return a void;

CPP
I created a class App;
In the class I created, I absorbed the variables defined in structure s refer to above).

In the new CPP *.h files, what do I write? put some function prototypes in the App definition under public:, and the static functions under private:.

In the CPP source I tried various combinations such as
<
private int classname::function1cpp( int y){
int y1; .....
return y1;
}
>
My reference material (google, text book) do not present a few lucid examples.
I have written a small application in C. I have a header (app.h) in which I declared a structure, and I include the function prototypes. In the app.c file I have several functions that manipulate that structure.

I want to convert the code to CPP. In the c source I think that the static functions that should become private, and other functions should be public.

C EXAMPLE
< static int function1(structure s, int y){
int y1; .....
return y1;
}>

I have other functions that are not static. Some return a value, some return a void;

CPP
I created a class App;
In the class I created, I absorbed the variables defined in structure s refer to above).

In the new CPP *.h files, what do I write? put some function prototypes in the App definition under public:, and the static functions under private:.

In the CPP source I tried various combinations such as
<
private int classname::function1cpp( int y){
int y1; .....
return y1;
}
>
My reference material (google, text book) do not present a few lucid examples.
Last edited on
Normally in the .h file you declare the class.
1
2
3
4
5
6
7
class App
{
public:
    void function1cpp( int y) ;
private:
   int y1; 
};


In the .cpp file you implement the class.
1
2
3
4
void App::function1cpp(int y)
{
   // do something
}


Thank you Thomas.

What about private functions? How do I define a private function()?

If the function1cpp prototype is in the private area of the class, does your example result in the creation of a private version?
Sorry, Either I did not follow the example in the link, or I am not looking at the link carefully enough.

Look at what I produced below. It is an example of a public and a private function in the App.h file
but in the App.cpp there is no distinction between public and private.



1
2
3
4
5
6
7
8
9
10
// app.h
class App
{
public:
    void function1cpp( int y) ;
private:
    void myprivate(int x);
   int y1; 
};







What do I have to add to the following to code in the App.cpp to tell the compiler that myprivate(int x) is private?

Will, because of the *App.h file prototype, result in myprivate() being identified as being a private function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

// app.cpp
//
void myprivate(int x)
{
//   do something without having public access
//  callable from another member function within the class.
}

void App::function1cpp(int y)
{
   // do something with public access
}
 
What do I have to add to the following to code in the App.cpp

Nothing.

The definition of the class in the app.h does already set the access rules.


In the linked page:
The name of every class member (static, non-static, function, type, etc) has an associated "member access". When a name of the member is used anywhere a program, its access is checked, and if it does not satisfy the access rules, the program does not compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
class Example {
 public: // all declarations after this point are public
    void add(int x) { // member "add" has public access
        n += x; // OK: private Example::n can be accessed from Example::add
    }
 private: // all declarations after this point are private
    int n = 0; // member "n" has private access
};
int main()
{
    Example e;
    e.add(1); // OK: public Example::add can be accessed from main
//  e.n = 7;  // Error: private Example::n cannot be accessed from main
}


Thank you Keskiverto

The rules you gave me were for data. I understood that concept before I asked my question.

I a, not aware that the rules also applied to functions defined for private ( a private function hidden from public and located in the private area), but declared within in a separate compile unit.

What do I have to do within app.cpp (my version) to correctly compile the myprivate() function.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	

////////////////////////////////The app.h header file///////////////////

// app.h
class APP
{
public:
   void function1cpp( int y) ;
private:
   void myprivate(int x);
   int y1; 
};

///////////////// end app.h /////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
//          SEPARATE FILE  app.cpp                                 //
//////////////////////////////////////////////////////////////////////////////////////////
//app.cpp
#include <iostream>
#include "app.h"

 //app.cpp

//how do I write the code for this one following function so it is known as private?
// Do I have to include code in the header as the only way?

 void myprivate(int x)
 {
 //   do something without having public access
 //  callable from another member function within the class.

 }

void APP::function1cpp(int y)
{
    // do something with public access to the private function
    myprivate( 3);
}

int main(void)
{
  APP Example;

  Example.function1cpp(3);
 }  

////////////// end app.cpp ////////////////////  




How do I code myprivate() so that the compiler knows it is a private function?
compiler is g++ --version is 5.3.1

Do I need a separate *.cpp file for private code and a second *.cpp file for public source code.
On line 9 you do declare that class APP has a public member function void function1cpp(int)
On lines 11-12 you do declare that class APP has a private member function void myprivate(int) and private member variable int y1

Lines 36-40 provide implementation for the member function1cpp(int) of APP.

Lines 29-34 declare and implement a standalone function void myprivate(int)

Your code does not include implementation for the member myprivate(int) of APP. It would look like:
1
2
3
4
5
void APP::myprivate(int x)
 {
   //   do something without having public access
   //  callable from another member function within the class.
 }



The implementation of a member function can be provided either within definition of the class:
1
2
3
4
5
6
7
class Foo {
  // other code
  void bar( int x ) {
    std::cout << x;
  }
  // other code
};

.. or outside of the class definition:
1
2
3
4
5
6
7
8
9
class Foo {
  // other code
  void bar( int x );
  // other code
};

void Foo::bar( int x ) {
  std::cout << x;
}

The definition of the class and only the definition determines whether the member is public, protected, or private.

You do not need separate source files.
I have some 50 functions of which 25 are to be private.


Within my C language makefile am I able to provide individual source files Each source file has functions that call static C functions? The static C functions never show up within the modules meta data (linker information).

For conversion to C++, can you show me a header file with a class prototype having public and private member functions and a separate source file showing the code for each of these public and private functions.

When your entire program is one executable with one source program, there is no problem as your own class definition is embedded within that source.

Am I wrong to say that irrespective of project size, the private functions have to be included into the Header file within the class definition?


APP.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef APP_H
#define APP_H

class APP
{
public:
   void function1cpp( int y );
private:
   void myprivate( int x );
};
#endif 


APP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "APP.h"

void APP::function1cpp(int y)
{
    myprivate( y + 3 );
}

void APP::myprivate(int x)
{
    std::cout << x << '\n';
}


example.cpp
1
2
3
4
5
6
7
8
#include "APP.h"

int main()
{
  APP Example;

  Example.function1cpp( 7 );
}

Thank You Keskirvo.
Your explanation


void APP::myprivate(int x)
{
// do something without having public access
// callable from another member function within the class.
}

Is what I was missing (brain fog).
I have many source code files, currentlly with static c functions. These are static as a way to have private code in a C api. In my CPP equivalent, these will be placed into the private area.

Can we mark this question as solved?
Topic archived. No new replies allowed.