Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

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;
}



Basic program Classes C++

Basic program Classes C++.

#include <iostream>
using namespace std;

class a{
public:
void set(int x){num = x;}
int get(){return num;}
private:
int num;
};

int main(){
a b;
b.set(45);
cout << b.get();
return 0;
}

Recursion Question

What will be the output?
#include <iostream>
using namespace std;
void f(int);
int main()
{
 f(2);
 return 0;
}
void f(int n)
{
 if (n == 0)
  cout << n << ' ';
 else
  f(n-1);
}
Reply in comments.

C++ basic percentage program

/*
WRITE A PROGRAM THAT WILL ASK SOMEONE HOW THEY SPEND THEIR DAY. aT LEAST 5 QUESTIONS:
HOW MANY HOUR DO YOU SPEND SLEEPING,
HOW MANY HOURS FO SPEND EATING,
DRINKING,
WALKING,
WORKING,.
CALCULATE PERCENTAGES
THE OUTPUT SHOULD LOOK SOMETHING LIKE THIS.
SLEEPING EATING DRINKING WALKING WORKING
40.00% 40.00% 7.00% 4.00% 9.00%
*/



#include <iostream>
using namespace std;

int main()
{
cout << "You spend time (in hours) in 1 day: " <<endl;
cout << "In Playing: "; int play; cin >> play;
cout << "In Sleeping: "; int sleep; cin >> sleep;
cout << "In Eating: "; int eat; cin >> eat;
cout << "In Working: "; int work; cin >> work;

cout << "In percentage: " << endl;
cout << "Playing: " << play/24.0 * 100 << "%" << endl;
cout << "Sleeping: " << sleep/24.0 * 100 << "%" << endl;
cout << "Eating: " << eat/24.0 * 100 << "%" << endl;
cout << "Working: " << work/24.0 * 100 << "%" << endl;

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