Need a project done?

C++ Programming Developer

Search This Blog

Write in a file C++


#include <iostream>
#include <fstream>//Used for file handling.

using namespace std;

int main(){
ofstream outfile;
outfile.open("rehan.txt");//This file will be created in the directory in which you've created your project.

outfile << "My name is Rehan\nand I love to play football" << endl;
cout << "File Written" << endl;

return 0;
}
See also:

Read from a file C++

Multiple inheritance basic program C++


Multiple inheritance basic program in C++

#include <iostream>
using std::cout;

class b1{
int x;
public:
b1(int = 0);
int get();
};
b1::b1(int u){
x = u;
cout << "B1 constructor\n";
}
int b1::get(){
return x;
}

class b2{
int x;
public:
b2(int = 0);
int get();
};
b2::b2(int u){
x = u;
cout << "B2 constructor\n";
}
int b2::get(){
return x;
}

class a: public b1, public b2{
int x;
public:
a(int u = 0);
};
a::a(u){
x = u;
cout << "A constructor\n";
}

int main(){
a o;
reuturn 0;
}

Overloading Subscript operator [] C++


#include <iostream>
using namespace std;

class a{
public:
double& operator[](int index);
double get(int index);//returns value at required index.
private:
double array[20];
};

double& a::operator[](int index){
return array[index];
}

double a::get(int index){
return array[index];
}

void main(){
a obj;
obj[5] = 4;

cout << "obj[5] = " << obj.get(5) << endl;

double var = obj[5];
cout << "var = " << var << endl;
}

Composition C++


#include <iostream>
using std::cout;
using std::endl;

class b{
public:
void set_mem_b(int value){
b_mem = value;
}
int get_mem_b() const{
return b_mem;
}
private:
int b_mem;
};

class a{
public:
void set(int x, int mem_class_b){
num = x;
a_mem.set_mem_b(mem_class_b);
}

int get() const {return num;}

int get_mem_b() const {
return a_mem.get_mem_b();
}
private:
int num;
b a_mem;
};

int main(){
a obj;
obj.set(45,-7);
cout << "obj.get() = " << obj.get() << endl;
cout << "obj.get_mem_b() = " << obj.get_mem_b() << endl;
return 0;
}

Send recorded message

Go to any of the following websites:

Vocaroo, Voice4Mail, DialMyCalls, Tokbox Voice Messenger, Cloud Talk and GoogleTalk Voicemail.

Constant objects require constant functions C++


#include<iostream>
using namespace std;

class A{
int i;
public:
A(){ i = 0; };
int c_get_i() const{ return i; }
int nc_get_i(){ return i; }
};

int main(){
const A c_a;   // c_a is a const object
A nc_a;

c_a.c_get_i();
//c_a.nc_get_i(); // This would give error if uncommented because const objects require const functions

nc_a.c_get_i();
nc_a.nc_get_i();
}

Written by Dr. Shahzad Rajput

Linked List using classes C++

Linked List using classes, inserting at tail in C++:

#include <iostream>
using namespace std;

Function Overloading in C++


Example of Function Overloading in C++:

#include <iostream>
using std::cout;
using std::endl;

class a{
public:
long double p_calculateArea(double);
private:
long double calculateArea(double);
};
long double p_a::calculateArea(double r){
return (calculateArea(r));
}
long double a::calculateArea(double r){
return (3.14159265359*r*r);
}
int main(){
a obj;
while (1){
cout << "Choice = 1, to calculate area of circle from the taken input arguments."
<< "\nChoice = 2, to calculate area of circle from the taken input arguments."
<< "\nChoice = 3, to calculate area of triangle from the taken input arguments."
<< "\nChoice = 4, Exit the Program.
int opt; cin >> opt;
switch(opt){
case 1:
cout << "Radius of Circle: "; double r; cin >> r;
cout << "Area of Circle: " << obj.p_calculateArea(r) << endl;
break;
case 2:

}
a::a(){
ID++;
}
int getUniqueID(){
return ID;
}

Use of Static in Classes C++

Use of static in Classes in C++ can bee seen through following program.


#include <iostream>
using std::cout;
using std::endl;
using std::cin;
class UniquelyIdentified{
public:
UniquelyIdentified();
const int getUniqueID() const;
private:
static int ID;
};
int UniquelyIdentified::ID = 0;
int main(){
cout << "Press 'c' to create new Object, -1 to exit" << endl;
char opt;
while ((cin>>opt) && (opt=='c')){
UniquelyIdentified obj;
cout << "Get ID? (y/n) ";
char o;
if ((cin >> o) && (o=='y')){
cout << "ID of current Object: " << obj.getUniqueID() << endl;
}
}
return 0;
}
UniquelyIdentified::UniquelyIdentified(){
ID++;
}
const int UniquelyIdentified::getUniqueID() const{
return ID;
}

Overload << operator C++

Overload << Operator in C++.

#include <iostream>
using namespace std;

class a{
friend ostream& operator<<(ostream out, a& obj);

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

ostream& operator << (ostream& out, a& obj){
out << "obj.x = " << obj.x << endl;
return out;
}

int main(){
a obj1, obj2;
obj1.setX(5);//Functions are called over objects.
cout << obj1;
obj2.setX(10);
cout << obj2;
return 0;
}

//If this Code doesn't work in some compiler, use following code:


Prime number Check Program C++


#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(){
bool isPrime = true;
cout << "Enter a number: "; int num; cin >>num;

for (int i = 2; i<num; i++){
if (num % i == 0){
isPrime = false;
break;
}
}
cout << num << " is " << (isPrime? "":"not") << " a prime number" << endl;
return 0;
}

See Also:

How to Calculate Prime Factors?

Prime Number Checker in C++


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