Need a project done?

C++ Programming Developer

Search This Blog

Throw class C++

Throw Class in C++ program is given below:

 #include <iostream>  
 //#include <string>  
 using namespace std;  
   
 class divByZero{};  //An empty CLASS.
   
 int main(){  
      try{  
      throw divByZero();  
      }  
      catch(divByZero){  
           cout << "divByZero caught in catch block" << endl;  
      }  
      return 0;  
 }  


Another throw Class in C++ program is given below:

 #include <iostream>  
 #include <string>  
 using namespace std;  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 class divByZero{  
 public:  
      divByZero();  
      divByZero(string);  
      string what();  
 private:  
      string message;  
 };  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 divByZero::divByZero(){  
      message = "Division by Zero IS NOT possbile";  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 divByZero::divByZero(string s){  
      message = s;  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 string divByZero::what(){  
      return message;  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 int main(){  
      try{  
      throw divByZero();  
      }  
      catch(divByZero s){  
           cout << "divByZero caught in catch block" << endl;  
             
           cout << s.what() << endl;  
      }  
      return 0;  
 }  


Another throw Class in C++ program is given below:
 #include <iostream>  
 #include <string>  
 using namespace std;  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 class divByZero{  
 public:  
      divByZero();  
      divByZero(string);  
      string what();  
 private:  
      string message;  
 };  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 divByZero::divByZero(){  
      message = "Division by Zero IS NOT possbile";  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 divByZero::divByZero(string s){  
      message = s;  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 string divByZero::what(){  
      return message;  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 void dosomething();  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 int main(){  
      dosomething();  
      cout << "Control Comes back in main" << endl;  
      return 0;  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  
 void dosomething(){  
      try{  
      throw divByZero();  
      cout << "Control Doesn't come back HERE" << endl;   
      }  
      catch(divByZero s){  
           cout << "divByZero caught in catch block" << endl;  
             
           cout << s.what() << endl;  
      }  
 }  
 /////////////////////////////////////////////////////////////////////////////////////////////////  

Linked List Write in file C++


 #include <iostream>  
 #include <fstream>  
 using namespace std;  
   
 ofstream os("linkedList.txt",ios::binary);  
   
 struct link{  
  int data;  
  link* next;  
 };  
   
 class linkedList{  
 public:  
  linkedList();  
  void addItem(int);  
  void print(void);  
 private:  
  link* first;  
 };  
   
 linkedList::linkedList(){  
  first = NULL;  
 }  
   
 void linkedList::addItem(int n){  
  link* temp = new link;  
  temp->data = n;  
  temp->next = first;  
  first = temp;  
  os.write(reinterpret_cast<char*>(temp),sizeof(temp));  
 }  
   
 void linkedList::print(){  
  for (link* p = first; p != NULL; p = p->next){  
  cout << p->data << endl;  
  }  
 }  
   
 int main(){  
  int n;   
   
  linkedList ll;  
   
  do{  
  cout << "Enter number to insert: (-1 to print): "; cin >> n;  
    
  ll.addItem(n);  
  } while (n != -1);  
    
  ll.print();  
  return 0;  
 }  

Exception bad_alloc C++


1:  #include <iostream>  
2:  using namespace std;  
3:  #include <string>  
4:    
5:  int main(){  
6:       int *list[100];  
7:    
8:       try{  
9:            for (int i = 0; i<100; i++){  
10:                 list[i] = new int[50000000];  
11:                 cout << "Created List[" << i << "] of 50000000 components." << endl;  
12:            }  
13:       }  
14:       catch(bad_alloc be){  
15:            cout << "In the bad_alloc catch bloc: " << be.what() << endl;  
16:       }  
17:       return 0;  
18:  }  

Exception out_of_range C++


1:  #include <iostream>  
2:  using namespace std;  
3:  #include <string>  
4:    
5:  int main(){  
6:       string sentence;  
7:       string str1, str2, str3;  
8:    
9:       try{  
10:            sentence = "Testing string exceptions!";  
11:            cout << "Sentence = " << sentence << endl;  
12:            cout << "sentence.lenght() = " << static_cast<int>(sentence.length()) << endl;  
13:            str1 = sentence.substr(8,20);  
14:            cout << "str1 = " << str1 << endl;  
15:            str2 = sentence.substr(28,10);  
16:            cout << "str2 = " << str2 << endl;  
17:            str3 = "Exception handling. " + sentence;  
18:            cout << "str3 = " << str3 << endl;  
19:            cout << "str3.length() = " << str3.length() << endl;  
20:       }  
21:    
22:       catch (out_of_range re){  
23:            cout << "In the out_of_range catch block: " << re.what() << endl;  
24:       }  
25:       catch(length_error le){  
26:            cout << "In the length error code block: " << le.what() << endl;  
27:       }  
28:    
29:       return 0;  
30:  }  

Linked list using Classes

Linked List using Classes program is given below:
Insert at head.

1:  #include <iostream>  
2:  using namespace std;  
3:  //////////////////////////////////////////////////////////////////////////////////////////////////  
4:  //////////////////////////////////////////////////////////////////////////////////////////////////  
5:  template <class TYPE>  
6:  struct link{//Within this struct definition 'link' means link<TYPE>  
7:       TYPE data;  
8:       link* next;  
9:  };  
10:  //////////////////////////////////////////////////////////////////////////////////////////////////  
11:  //////////////////////////////////////////////////////////////////////////////////////////////////  
12:  template <class TYPE>  
13:  class ll{//ll => linked list  
14:       private:  
15:            link<TYPE> *first;  
16:       public:  
17:            ll();  
18:            void addItem(TYPE);  
19:            void display();  
20:  };  
21:  //////////////////////////////////////////////////////////////////////////////////////////////////  
22:  template <class TYPE>  
23:  ll<TYPE>::ll(){  
24:       first = NULL;  
25:  }  
26:  //////////////////////////////////////////////////////////////////////////////////////////////////  
27:  template <class TYPE>  
28:  void ll<TYPE>::addItem(TYPE n){  
29:       link<TYPE>* temp= new link<TYPE>;  
30:       temp->data = n;  
31:       temp->next = first;  
32:       first = temp;  
33:  }  
34:  //////////////////////////////////////////////////////////////////////////////////////////////////  
35:  template <class TYPE>  
36:  void ll<TYPE>::display(){  
37:       link<TYPE>* p = first;  
38:       for (; p!= NULL; p=p->next){  
39:            cout << p->data << endl;  
40:       }  
41:  }  
42:  //////////////////////////////////////////////////////////////////////////////////////////////////  
43:  //////////////////////////////////////////////////////////////////////////////////////////////////  
44:  //template <class TYPE>  
45:  int main(){  
46:       ll<char> l;  
47:       char n;  
48:       do{  
49:            cout << "NUMBER: (n to exit)";          cin >> n;  
50:            l.addItem(n);  
51:       }  
52:       while (n!='n');  
53:       l.display();  
54:       return 0;  
55:  }  

Linked List using Classes


Linked List using Classes program is given below:

1:  #include <iostream>  
2:  using namespace std;  
3:  //////////////////////////////////////////////////////////////////////////////////////////////////  
4:  //////////////////////////////////////////////////////////////////////////////////////////////////  
5:  template <class TYPE>  
6:  struct link{  
7:       TYPE data;  
8:       link* next;  
9:  };  
10:  //////////////////////////////////////////////////////////////////////////////////////////////////  
11:  //////////////////////////////////////////////////////////////////////////////////////////////////  
12:  template <class TYPE>  
13:  class linkList{  
14:  private:  
15:       link<TYPE>* first;  
16:  public:  
17:       linkList()  
18:       { first = NULL; }  
19:       void addItem(TYPE d);  
20:       void display();  
21:  };  
22:  //////////////////////////////////////////////////////////////////////////////////////////////////  
23:  //////////////////////////////////////////////////////////////////////////////////////////////////  
24:  template <class TYPE>  
25:  void linkList<TYPE>::addItem(TYPE d){  
26:       link<TYPE> *temp = new link<TYPE>;  
27:       temp->data = d;  
28:       temp->next = NULL;  
29:       if (first == NULL){  
30:            first = temp;  
31:       }  
32:       else{  
33:            temp->next = first;  
34:            first = temp;  
35:       }  
36:  }  
37:  //////////////////////////////////////////////////////////////////////////////////////////////////  
38:  template <class TYPE>  
39:  void linkList<TYPE>::display(){  
40:       link<TYPE> *p = first;  
41:       for (int i = 0; p!=NULL; p = p->next){  
42:            cout << p->data << endl;  
43:       }  
44:  }  
45:  //////////////////////////////////////////////////////////////////////////////////////////////////  
46:  //////////////////////////////////////////////////////////////////////////////////////////////////  
47:  int main(){  
48:       linkList<double> l;//Here you can write char, int, etc.  
49:       char opt;  
50:       do{  
51:            cout << "Number: ";          int n;          cin >> n;  
52:            l.addItem(n);  
53:            cout << "Do you want to insert any other item? (y/n) ";     cin >> opt;  
54:       }  
55:       while (opt == 'y');  
56:       l.display();  
57:       return 0;  
58:  }  

Template C++

Simple example of Template in C++.

1:  #include <iostream>  
2:  using namespace std;  
3:  template <class dT>  
4:  void swapIt(dT &u, dT &v);  
5:    
6:  int main(){  
7:       int i1 = 2, i2 = 4;  
8:       double d1 = 2.2, d2 = 5.6;  
9:       float f1 = 1.1, f2 = 5.2;  
10:       char c1 = 'c', c2 = 'R';  
11:       cout << "i1 = " << i1 << "\ti2 = " << i2 << endl;  
12:       cout << "d1 = " << d1 << "\td2 = " << d2 << endl;  
13:       cout << "f1 = " << f1 << "\tf2 = " << f2 << endl;  
14:       cout << "c1 = " << c1 << "\tc2 = " << c2 << endl;  
15:         
16:       swapIt(i1,i2);  
17:       swapIt(f1,f2);  
18:       swapIt(d1,d2);  
19:       swapIt(c1,c2);  
20:    
21:       cout << "\nAfter swaping: " << endl;  
22:       cout << "i1 = " << i1 << "\ti2 = " << i2 << endl;  
23:       cout << "d1 = " << d1 << "\td2 = " << d2 << endl;  
24:       cout << "f1 = " << f1 << "\tf2 = " << f2 << endl;  
25:       cout << "c1 = " << c1 << "\tc2 = " << c2 << endl;  
26:       return 0;  
27:  }  
28:  template <class dT>  
29:  void swapIt(dT &u, dT &v){  
30:       dT t = u;  
31:       u = v;  
32:       v = t;  
33:  }  

Find a particular person from file C++

C++ Program to find a particular person from a file using file handling.

1:  #include <iostream>  
2:  #include <fstream>  
3:  using namespace std;  
4:    
5:  /////////////////////////////////////////////////////////////////////////////////////////////////  
6:  class person{  
7:  protected:  
8:       char name[80];  
9:       short age;  
10:  public:  
11:       void getData(){  
12:            cout << "Enter name: "; cin.ignore(); cin.getline(name,80,'\n');   
13:            cout << "Enter age: "; cin >> age;   
14:       }  
15:       void showData(){  
16:            cout << "Name: " << name << endl;  
17:            cout << "Age: " << age << endl;  
18:       }  
19:  };  
20:  /////////////////////////////////////////////////////////////////////////////////////////////////  
21:  int main(){  
22:       person pers;  
23:       fstream fs("person.dat", ios::in | ios::out | ios::binary);  
24:       char opt;  
25:       do{  
26:            cout << "\nEnter person's data:\n";  
27:            pers.getData();  
28:            fs.write(reinterpret_cast<char*>(&pers),sizeof(person));  
29:            cout << "Any other person left? (y/n) "; cin >> opt;  
30:       }  
31:       while (opt == 'y');  
32:         
33:       cout << "\n\nWhich person's data do you want to get? "; int n; cin >> n;  
34:       fs.seekg(0,ios::end);  
35:       int endPosition = fs.tellp();  
36:       int totalPersons = endPosition/sizeof(person);  
37:       cout << "Total Persons are: " << totalPersons << endl;  
38:         
39:       fs.seekg((n-1)*sizeof(person));  
40:       fs.read(reinterpret_cast<char*>(&pers),sizeof(person));  
41:       pers.showData();  
42:         
43:       return 0;  
44:  }  

Input/Output with Multiple Objects C++


Write Objct to File C++

#include <iostream>
#include <fstream>
using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void getData(){
cout << "Enter name: "; cin.getline(name,80,'\n');
cout << "Enter age: "; cin >> age;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;
pers.getData();

//open file.
ofstream os("PERSON.DAT", ios::binary);
//write this object to file.
os.write(reinterpret_cast<char*>(&pers), sizeof(person));
cout << "FILE WRITTEN" << endl;
return 0;
}

Now

Read Object from File C++

Read Object from File C++

After an object has been written in a file, you can read that object from file in C++.
I've written an object at Write Objct to File C++.

After writing this object, following is the program to read an object from a file.

#include <iostream>
#include <fstream>
using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void showData(){
cout << "Name: " << name << endl;
cout << "Age: " <<  age << endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
person pers;

//open file.
ifstream is("PERSON.DAT", ios::binary);
//read an object from this file.
is.read(reinterpret_cast<char*>(&pers), sizeof(person));
//after reading object from file, Show it.
pers.showData();
return 0;
}

Write and read Object in file C++


Write and read Object in file in C++.

#include <iostream>
#include <fstream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////
class person{
protected:
char name[80];
short age;
public:
void getData(){
cout << "Enter name: "; cin >> name;
cout << "Enter age: "; cin >> age;
}
void showData(){
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
////////////////////////////////////////////////////////////////////////////////////////////
int main(){
person pers;
pers.getData();
ofstream os("person.txt", ios::binary);//Open file in binary mode.
cout << "This data has been store in the file person.txt in directory you've created your project" << endl;
cout << "Now Enter this data again, this data will not be saved" << endl;
os.write(reinterpret_cast<char*>(&pers), sizeof(pers));
pers.getData();
os.close();

//PRINTING SAVED DATA IN FILE.
ifstream is("person.txt",ios::binary);
is.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showData();//Here the data saved in file should have been displayed, but it's showing data not saved in file. WHY?
return 0;
}

Write files in binary C++


Write files in binary in C++.

#include <iostream>
#include <fstream>
using namespace std;

int main(){
int a[10];
for (int i = 0; i<10; i++){
a[i] = i;
}

ofstream os("REHAN.txt", ios::binary);
os.write(reinterpret_cast<char*>(a),10*sizeof(int));

os.close();
for (i = 0; i<10; i++){
a[i] = 0;
}

ifstream is("REHAN.txt",ios::binary);
is.read(reinterpret_cast<char*>(a),10*sizeof(int));
for (i = 0; i<10; i++){
cout << a[i] ;
}
return 0;
}

Read from a file C++


Program to read from a file in C++.
#include <iostream>
#include <fstream>

using namespace std;

int main(){
ifstream infile;
infile.open("rehan.txt");//rehan.txt must exist in the directory in which you've created your project.

char a[40];
while (infile){
infile.getline(a,40,'\n');
cout << a << endl;
}
infile.close();
return 0;
}
See also:

Write in file C++


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;
}
"Don't let anyone ever make you feel like you don't deserve what you want."