Need a project done?

C++ Programming Developer

Search This Blog

Basic Classes C++


#include <iostream>
using namespace std;

class a{
int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}
};

int main(){
a obj;
obj.setX(10);//Functions are called over objects.
cout << "x = " << obj.getX() << endl;
return 0;
}

Another way

//-----------------------------Seperating in 3 files-----------------------------------------------------------------

//test.h
class a{
int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}

void copyConstructor(a &obj);
};
//-----------------------------------

//test.cpp
#include "test.h"

void a::copyConstructor(a &obj){
x  =  obj.getX();
}
//----------------------------------
//Main.cpp
#include <iostream>
#include "test.h"
using namespace std;

int main(){
a obj;
obj.setX(10);//Functions are called over objects.
cout << "x = " << obj.getX() << endl;
a obj1;
obj1.copyConstructor(obj);
cout << "obj1 = " << obj1.getX() << endl;
return 0;
}



"Don't let anyone ever make you feel like you don't deserve what you want."