Need a project done?

C++ Programming Developer

Search This Blog

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++


Vowel words counter C++


#include <iostream>
using namespace std;

void main(){
int c = 0;
char a[100];
cin >> a;
for (int i = 0 ; i<100; i++){
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') {
c++;
}
}
cout << c << endl;
}

Prime numbers C++


#include <iostream>
using namespace std;

int main(){
bool check = true;
//All prime numbers till 100.
for (int num = 2; num<=100; num++){
for (int j=2; j<num; j++){
if (num%j == 0){
check = false;
}
}
if (check == true){
cout << num << endl;
}
check = true;
}
return 0;
}

Display all divisors of a number C++


C++ Display all divisors of a number.

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

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

for (int i = 2 ; i < num;  i++){
if (num % i == 0){
cout << i << endl;
}
}
return 0;
}

Operator Overloading C++

//Adding two objects of different classes.

//This example of operator overloading uses friend functions and friend class to set and get the value of 'x', a private member.
//Before this example, see an easier example.

#include <iostream>
using namespace std;

class b{
int x;
public:
friend void setX(b& obj_b, int value);
friend int getX(b& obj_b);
friend class a;
};
class a{
int x;
public:
friend void setX(a& obj_a, int value);
friend int getX(a& obj_a);
int operator+(b &obj);
};

int main(){
a obj_a;
b obj_b;
setX(obj_a,5);
setX(obj_b,10);
cout << "obj_a = " << getX(obj_a) << endl;
cout << "obj_b = " << getX(obj_b) << endl;
cout << "\nSUM of objects of different classes: " << obj_a + obj_b << endl;
return 0;
}

int a::operator+(b &obj){
return x + obj.x;
}

void setX(a& obj_a, int value){
obj_a.x = value;
}
void setX(b& obj_b, int value){
obj_b.x = value;
}
int getX(a& obj_a){
return obj_a.x;
}
int getX(b& obj_b){
return obj_b.x;
}

Operator Overloading C++

Operator Overloading C++
Adding two objects of same class using + sign.


#include <iostream>
using namespace std;
class a{
int x;
public:
void setX(int);
int getX();
int operator+ (a &obj);
};
int main(){
a obj1, obj2;
obj1.setX(5);
cout << "Obj1.x = " << obj1.getX() << endl;
obj2.setX(10);
cout << "Obj2.x = " << obj2.getX() << endl;
  cout <<"\nObj1+Obj2 = " << obj1+obj2 << endl;
return 0;
}

int a::operator+ (a& obj){
return (x + obj.x);
}
void a::setX(int value){
x = value;
}
int a::getX(){
return x;
}

Another Example.

Friend Class C++


#include <iostream>
using namespace std;

class a{
friend class b;//a has granted friendship to b.
public:
//public members...
private:
int a_mem;
};

class b{
public:
int get_member_of_class_a(a &instance_of_a){
return instance_of_a.a_mem; //Since b is a friend of a so b should have access
//to all members of class a's objects.
//Without using friend statement in class a, we cannot access private member of class 'a' in class 'b' using object.private_member.
}
void set_member_of_class_a(a& instance_of_a,int value){
instance_of_a.a_mem = value;
}
private:
//private members...
};

int main(){
b obj_b;
a obj_a;
cout << "We access class a's private member through class b << endl:
obj_b.set_member_of_class_a(obj_a,5);
cout << obj_b.get_member_of_class_a(obj_a) << endl;
return 0;
}

Friend Function C++

Friend Function in C++ Program is given below:


//Friend Function program in C++
#include <iostream>
using namespace std;

class a{
friend void f(a &obj);
public:
int getData(){
return a_mem;
}
private:
int a_mem;
};

void f(a &obj){//This function being friend function has the access to private members of passed object (obj) of class 'a'.
obj.a_mem = 5; //You cannot do a_mem = 5; without passing any argument, since function is not a member of class.
}


int main(){
a obj;
f(obj);
cout << obj.getData() << endl;
return 0;
}

Complex number Classes C++


Question:
Complex numbers are of the form where x and y are real numbers and i is the imaginary x + iy, unit equal to
√− 1 and i2 =− 1 . Very often a complex number is represented by a single letter, z = x + iy.
The complex conjugate of a complex number z = x + iy is defined to be: z = x − iy.
Operations on Complex Numbers:
Consider, z1 = x1 + iy1 and
z iy . 2 = x2 + 2
Addition: z1 + z2 = (x1 + x2) + i(y1 + y2)
Subtraction: z1 − z2 = (x1 + x2) − i(y1 + y2)
Multiplication: z1. z2 = (x1x2 − y1y2) + i(x1y2 + x2y1)
Division:          
The Complex Class:
Implement the class named Complex. The class should contain two data members, real and imag, both of
which are doubles. It should also have the following member functions:
1. Complex() : the default constructor that sets both real and imag to zero.
2. Complex( double r ) : an overloaded constructor that sets real to r and imag to zero.
3. Complex( double r, double i ) : another overloaded constructor that sets real to r and imag to i.
4. Complex( Complex c ) : the copy constructor
5. Complex add( Complex c ) : this function adds the complex number invoking the function to c (the
parameter to this function), and returns a new complex number that represents their sum.
6. Complex subtract( Complex c ) : this function subtracts the complex number c from the complex
number invoking the function, and returns a new complex number that represents their difference.
7. Complex multiply( Complex c ) : this function multiplies the complex number invoking the function
to c, and returns a new complex number that represents their product.
8. Complex divide( Complex c ) : this function divides the complex number invoking the function to c,
and returns a new complex number that represents the quotient.
9. void print() : the function that prints the complex number object. For eg., if, for the given object, the
value of real is 2.4 and that of imag is 3.7, this function should print 2.4 + 3.7i on the screen.
Apart from these functions, you should also define the getter and setter functions: getReal, getImag, setReal
and setImag.
Test the program by writing the main() function. Inside this function, you should prompt the user to enter real
and imaginary parts of two complex numbers ( and ). Use the operations defined in the z1 z2 class to perform the addition ( z1 + z2 ), subtraction ( z1 − z2 ),multiplication ( z1. z2 ), and division z2.

Solution:

Copy Constructor C++



Initializes a new object from another, existing one
Signature:
Class::Class(Class &obj)
{
}
Date::Date(Date &date)
{
// no need to check passed date arg
  month = date.month;
  day   = date.day;
  year  = date.year;
}

//Example



#include <iostream>
using namespace std;

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

void copyConstructor(a &obj);
};

void a::copyConstructor(a &obj){
x  =  obj.x;
}

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



Sorting linked list in C++

Linked list has been sorted alphabetically in the following program.

struct pen{
char* color;
Int qty;
pen* link;
}
Q1. Using the above structure you are required to manage the linked list considering the
following rules.
Pen should be stored in alphabetical order.
* If you have two pens of color red and green, your linked list will be as follows:
head
* If you have 5 pens, Aqua, Green, Blue, Red and yellow, your linked list will be:
head
● Your program should display the menu:
Select your choice
1. Add Pen
2. Sell Pen
3. Display Stock
//------------------------------------------------------------------------------------------
Solution:

initializing character array Structs

Initialize character array in struct:

I've seen that there arises problem when initializing character type array in structs. Let me tell you the correct way.

#include <iostream>
using namespace std;

struct a{
double id;
int tag;
char array[10];
};

int main(){
a var1, var2;
var1.id = 100.0;
var1.tag = 1;
var1.array = "The City"; //WRONG.
//Correct: strcpy(var1.array,"The City");
var2 = var1;
cout << "var2.id = " << var2.id << endl;
cout << "var2.tag = " << var2.tag << endl;
cout << "var2.array = " << var2.array << 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;
}
"Don't let anyone ever make you feel like you don't deserve what you want."