How to move code fragment to a diffrent file in c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm writing in C++ and my code is getting bigger and bigger. I wonder if I could somehow move code fragment to a different file. Let's say i have:
class First {
class Second
{
void a();
void b();
};
void c();
};
I would like to move my Second class to a Second.h file and then of course write bodies of Second class and functions into Second.cpp file. How can I do this?

Given that class Second is nested in class First you could start by refactoring your code like so:
class First
{
class Second;
void c();
};
class First::Second
{
void a();
void b();
};
From there it is easy to break the two classes into separate files:
First.h:
class First
{
class Second;
void c();
};
And Second.h
#include "First.h"
class First::Second
{
void a();
void b();
};
And then your .cpp files would be:
First.cpp
#include "First.h"
void First::c()
{
}
Second.cpp
#include "Second.h"
void First::Second::a()
{
}
void First::Second::b()
{
}

Lets assume you have First.h
class First{
class Second
{
void a();
void b();
};
void c();
};
you can provide the implementation of class Second in a separate translation unit:
like
First.cpp:
#include "First.h"
void First::c() {
}
Second.cpp:
#include "First.h"
void First::Second::a() {
}
void First::Second::b() {
}
I'm not sure that's what you're after though.

Related

I want to refactoring this code (default parameter using non-instantiated class)

my_func.h
class MyClass{
public:
OtherClass otherclass;
void func();
void func(OtherClass others);
};
my_func.cpp
#includ <my_func.h>
void MyClass::func(){
func(this->otherclass);
}
void MyClass::func(OtherClass others){
if(others.value.IsObject)
func(others.value);
}
main.cpp
#include <my_func.h>
int main(){
func();
}
I want to refactoring this code.
I want to set default parameter, so I write code like this.
but I think there is a better way.

Static member functions in namesapce [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have static member function inside my class which I would like to add in my namespace.
class A{
public:
static void func();
};
namespace myNamespace{
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
I get the following errors:
error: definition of ‘void A::func()’ is not in namespace enclosing ‘A’ [-fpermissive]
error: ‘myNamespace::A’ has not been declared
myNamespace::A::func();
void A::func(){
what is the reason?
because you put a function that belongs to the class A inside namespace. just make the class and function A together inside namespace your code should be like this
namespace myNamespace {
class A {
public:
static void func();
};
void A::func() {
}
}
int main()
{
myNamespace::A::func;
return 0;
}
The declaration and the definition both should be inside the NameSpace for it to work. Here in your code, you have declared the class outside the namespace. You can fix it as follows.
namespace myNamespace{
class A{
public:
static void func();
};
void A::func(){
...
}
}
int main(){
myNamespace::A::func;
}
Also try to understand the error messages which would save you a lot of time googling.
https://www.crashhandler.com/ ---> Best Practices in C++ {EDIT :: A blog by myself}

How do i Declare methods in public and write the code in private [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have been given this code with the restraints that i can not edit the public in anyway and am not allowed to call any other library's other than the ones specified.
Class example
{
private:
vector<int> vector_1;
void function_1(string name)
{
"code for function one goes here"
}
public:
void function_1(string name);
};
is there way to map function_1 to a method in private? so that in main: a method call would act like it was it was in public however it is in private (considering the constraints)
-edit: i have read the rules and research a bit however could not find a true answer to what i was looking for. Here is the code sample:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
{
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput();
void Output(string input)
};
int main(void) {
MapFunction a;
return 0;
}
The restraints of my solution is that i am not allowed to make changes to the public section of the class or put code into main. Normally i would create the method inside of the class by creating a method like this
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class MapFunction
{
private:
string responce;
string input;
vector<int> templist;
public:
void AskForInput(void);
void Output(void);
};
void MapFunction::AskForInput() {
cin >> input;
};
void MapFunction::Output() {
cout << input;
};
int main(void) {
MapFunction a;
a.AskForInput();
a.Output();
return 0;
}
however i wondering if it was possible to place the methods inside private and allowing main to access them without changing the way it is called in public
A member function always has access to all members (private and public ones), no matter if it is private or public itself. So a function cannot behave as "in private" or "in public", it just behaves as "function".
Private/public only is of relevance for accessing the function from outside the class – either one can (public) or cannot (private) (and additionally, there's yet protected, which is like private except for inheriting classes).
Finally: There's yet the friend keyword: By declaring a function (or a class) friend, you give it access to the private members of the class. This access comprises all members, though, you cannot give access just to specific members (at least not without tricks such as proxy classes). Example:
void outerFriend();
void outer();
class C
{
public:
void publicInner();
private:
friend void outerFriend();
void privateInner();
};
void outer()
{
C c;
c.publicInner(); // legal
c.privateInner(); // not accessible!
}
void outerFriend()
{
C c;
c.publicInner(); // legal
c.privateInner(); // legal(!)
}

alias vs empty new extending class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edit 2:
Ok so i might rephrase the question, is there a differense between an empty extending class and its base class, apart from beeing a different type? Like is the memory-size the same, and are they functionally equivalent?
Is there any difference in the compiled code and/or performance of this completely empty class definition versus an alias? Or does this depend on the compiler, in which case, it it likely to be optimised away?
class MyClass : MyTemplateClass<int>{};
using MyClass = MyTemplateClass<int>;
I want to use the class definition so I can forward declare it and avoid a circular dependency more easily.
The circular dependency:
Master.h
class Master {
void run();
State s;
}
State.h
class State {
void Modify(MyClass&);
}
MyTemplate.h
template<typename T>
class MyTemplateClass<int> {
void run(Master* pMaster) {
pMaster->run();
}
};
using MyClass = MyTemplateClass<int>;
"State.h" needs "MyTemplate.h" to be included, "Template.h" needs "Master.h" to be included, and "Master.h" needs "State.h" to be included, completing the circle. If MyClass was not an alias but an actual class, State would not need to include the template and can forward declare MyClass.
Let's see:
#include <iostream>
#include <typeinfo>
template<class T> struct MyTemplateClass {};
class MyClass1 : MyTemplateClass<int>{};
using MyClass2 = MyTemplateClass<int>;
int main()
{
std::cout << typeid(MyClass1).name() << std::endl;
std::cout << typeid(MyClass2).name() << std::endl;
}
example output:
8MyClass1
15MyTemplateClassIiE
Different types.

Inheriting classes and namespaces [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I want to do now is to make changements in FUNC.cpp and FUNC.h to inherit it to main.cpp and then, generate a diagram class at the end in which is really states that FUNC is inherited.
I want to make changements in my code, from namespace to classes, to allow the inheritance process.
I ' m having what is follow:
In a FUNC.h:
namespace FUNC
{
void f1(...);
void f2(...);
}
In a FUNC.cpp
namespace FUNC
{
void f1(...)
{
}
void f2(...)
{
}
}
in test.cpp (which is meantime a class an having its test.h) , it's possible to call f1 and f2 as follow:
FUNC::f1(...);
FUNC::f2(...);
If you change the namespace to a class, but still want to call func1 and func2 using the same syntax (e.g. FUNC::func1()), you have to make the functions static:
struct FUNC
{
static void func1();
static void func2();
};
If you want to override func1 in an inherited class then it's simple:
struct FUNC2 : public FUNC
{
static void func1();
};
There are however problems with static member functions, in that they can't access non-static members easily.
I will answer my own question
I had to insert after
class FUNC: public TEST
{
static void func1();
static void func2();
};