C++ Assignment Help

Discuss your C++ Assignment,free of cost, with our highly qualified C++ Programming Expert Tutors.Only after you are convinced with the discussion with our Online C++ Programming Tutors,you may make the payment for the solution of your assignment.The objective behind offering this unique opportunity to discuss with the expert even before making the payment is to enable the student to specify the requirements of his C++ Homework Assignment in a detail manner which is not always possible by email based communication.Our C++ Help Tutors not only aim to provide high quality Programming Solution to your C++ Homework,they also strictly adhere to the guidelines and timelines provided by you so that the solution fetches you the best grade.Moreover,all your C++ Assignment Code are well commented so that you can easily understand it.

You can also schedule Online C++ Tutoring Session with our Online C++ Programming Tutors.

1. Formatted I/O using ios member functions

    Code Listing: 

// File name: ~ftp/pub/class/cplusplus/IO/IOMember.cpp

// Purpose:   Demonstrating formatted IO using ios member functions

#include <iostream>

using namespace std;

int main()

{

           float num[5] = {1.0, -1.2345, 2350.1, 23.4, 45.34};

           int i;

           cout.setf(ios::showpos); // show the + sign before positive numbers

           cout.setf(ios::scientific); // use scientific notation

           cout.precision(2); // two digits after decimal point

      for(i = 0; i < 5; i++)

           {

                           cout.width(20);    // use 10 spaces for the number

                           cout.fill('$');    // pad with $

                          cout << num[i] << endl;

           }

  return 0;

}

    Running session:

mercury[148]% CC -LANG:std IOMember.cpp -o IOMember

mercury[149]% IOMember

$$$$$$$$$$$+1.00e+00

$$$$$$$$$$$-1.23e+00

$$$$$$$$$$$+2.35e+03

$$$$$$$$$$$+2.34e+01

$$$$$$$$$$$+4.53e+01

mercury[150]%

2. Formatted I/O using I/O manipulators

Code listing:

// File name: ~ftp/pub/class/cplusplus/IO/IOMember.cpp

// Purpose:   Demonstrating formatted IO manipulators

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

           float num[5] = {1.0, -1.2345, 2350.1, 23.4, 45.34};

           int i;

           cout << setiosflags(ios::showpos);

           cout << setiosflags(ios::scientific);

           cout << setprecision(2);

        

           for(i = 0; i < 5; i++)

           {

                           cout << setw(20);   

                           cout << setfill('$');   

                          cout << num[i] << endl;

           }

 

  return 0;

}

Running session:

mercury[156]% CC -LANG:std IOManip.cpp -o IOManip

mercury[157]% IOManip

$$$$$$$$$$$+1.00e+00

$$$$$$$$$$$-1.23e+00

$$$$$$$$$$$+2.35e+03

$$$$$$$$$$$+2.34e+01

$$$$$$$$$$$+4.53e+01

mercury[158]%

3. Define your own I/O manipulators

Code listing:

// File name: ~ftp/pub/class/cplusplus/IO/IOUserManip.cpp

// Purpose:   Demonstrates how to define an IO manipulator function

//            of your own.

 

#include <iostream>

#include <iomanip>

using namespace std;

 

// function prototype for a user-defined IO manipulator function

ostream &SetHex(ostream &stream);

 

int main()

{

           int input;

           cout << "Enter an integer: " ;

           cin  >> input;

 

           cout << "Hexdecimal is: ";

          

           // call the SetHex function so that the input

           // integer will be printed out in hexdecimal

           cout << SetHex << input << endl;

           return 0;

}

 

// definition of a user defined I/O manipulate function

ostream &SetHex(ostream &stream)

{

           // Set output base to be hexdecimal.

           // Use ios::basefield to make sure that all other

           // field in basefield is cleared before setting it

           // to hex.

           stream.setf(ios::hex,ios::basefield);

           return stream;

}

Running session:

mercury[200]% CC -LANG:std IOUserManip.cpp -o IOUserManip

mercury[201]% IOUserManip

Enter an integer: 19

Hexdecimal is: 13

mercury[202]%

 

4. File I/O: Writing to a text file

Code Lising

// File name: ~ftp/pub/class/cplusplus/IO/FileWrite.cpp

// Purpose:   Write to text file

 

#include <iostream>

#include <fstream>

using namespace std;

 

int main()

{

           char OutFile[80]; // output file name

           ofstream OutStream;  // open an output stream

          

           cout << "Please enter output file name: " ;

           cin >> OutFile;

 

           OutStream.open(OutFile, ios::out | ios::trunc);

          

           // make sure file is successfully opened

           if(!OutStream)

           {

                           cout << "Error open file " << OutFile << " for writing\n";

                           return 1;

           }

          

           // Write the following two lines to file

           OutStream<<"It is easier to resist at the beginning than at the end.\n";

           OutStream <<"                 -- Leonardo da Vinci" << endl;

 

           OutStream.close(); // close output stream

 

           cout << "Content written to " << OutFile <<".\n";

 

           return 0;

 

}

Running Session

mercury[298]% CC -LANG:std FileWrite.cpp -o FileWrite

mercury[299]% FileWrite

Please enter output file name: out.txt

Content written to out.txt.

mercury[300]%

 

5. File I/O: Reading from a text file

Code Lising

// File name: ~ftp/pub/class/cplusplus/IO/FileRead.cpp

// Purpose:   Read the content of a text file and print it

//            out on the screen

 

#include <iostream>

#include <fstream>

using namespace std;

 

int main()

{

           char InFile[80];  // input file name

           char ch;

          

           ifstream InStream;

 

           cout << "This program reads the content of a file and ";

           cout << "prints it out on the screen." << endl;

 

           cout << "Enter input file name: " ;

           cin >> InFile;

 

           // Open file for input

           // in.open(fin); also works

           InStream.open(InFile, ios::in);

 

           // ensure file is opened successfully

           if(!InStream)

           {

                           cout << "Error open file " << InFile << endl;

                           return 1;

           }

          

           cout << "Here is the content of " << InFile << ":\n";

           // Read in each character until eof character is read.

           // Output it to screen.

           while (!InStream.eof()) {

                           //Read each character.

                           InStream.get(ch);   

 

                           // make sure we don't write any odd characters on screen

                           if (!InStream.eof())

                           {

                                          cout << ch;  //Write to screen

                           }

           }

 

           InStream.close();

}

Running Session

mercury[243]% CC -LANG:std FileRead.cpp -o FileRead

mercury[244]% FileRead

This program reads the content of a file and prints it out on the screen.

Enter input file name: test.txt

Here is the content of test.txt:

It is easier to resist at the beginning than at the end.

                -- Leonardo da Vinci

 

mercury[245]%


6  A simple program demonstrating structure

    Code Listing: 

// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp

// Purpose:   Demonstrates the use of structure in C++.

//            Stores some personal data in a structure, then prints

//            the info out.

 

#include <iostream>

using namespace std;

 

int main()

{

        // Defining a structure

        struct PersonalData

        {

                char *FirstName;

                char *LastName;

                char *Birthday;  // in the format of 12/30/1978

                int  PhoneNum;

        }; // don't forget the ending ";"

 

        // Declaring a variable of type PersonalData

        PersonalData PersonOne;

 

        // Populate PersonOne with data

        PersonOne.FirstName = "John";

        PersonOne.LastName = "Doe";

        PersonOne.Birthday = "12/30/1978";

        PersonOne.PhoneNum = 5855555;

 

        // Print the data out

        cout << "PersonOne's First name is: " << PersonOne.FirstName << endl;

        cout << "PersonOne's Last name is: " << PersonOne.LastName<< endl;

        cout << "PersonOne's Birthday is: " << PersonOne.Birthday<< endl;

        cout << "PersonOne's Phone number is: " << PersonOne.PhoneNum<< endl;

 

        return 0;

}

 

    Running session:

@9:51am merc-hu[34]% CC -LANG:std StrucSimple.cpp -o StrucSimple

@9:52am merc-hu[35]% StrucSimple

PersonOne's First name is: John

PersonOne's Last name is: Doe

PersonOne's Birthday is: 12/30/1978

PersonOne's Phone number is: 5855555

@9:52am merc-hu[36]%

7. Nested structure

Code listing:

// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp

// Purpose:   Demonstrates the use of nested structure in C++.

//            Stores some personal data in a structure, then prints

//            the info out.

 

#include <iostream>

using namespace std;

 

int main()

{

        // Defining a structure for name

        struct Name

        {

                char *FirstName;

                char *LastName;

        };

 

        struct PersonalData

        {

                Name NameField; // struct as a memeber

                char *Birthday;  // in the format of 12/30/1978

                int  PhoneNum;

        }; // don't forget the ending ";"

 

        // Declaring a variable of type PersonalData

        PersonalData PersonOne;

 

 

        PersonOne.NameField.FirstName = "John";

        PersonOne.NameField.LastName = "Doe";

 

        // Populate PersonOne with data

        PersonOne.Birthday = "12/30/1978";

        PersonOne.PhoneNum = 5855555;

 

        // Print the data out

        cout << "First name is: " << PersonOne.NameField.FirstName << endl;

        cout << "Last name is: " << PersonOne.NameField.LastName<< endl;

        cout << "Birthday is: " << PersonOne.Birthday<< endl;

        cout << "Phone number is: " << PersonOne.PhoneNum<< endl;

 

        return 0;

}

 

Running session:

@9:59am merc-hu[45]% CC -LANG:std StructNested.cpp -o StructNest

@10:00am merc-hu[47]% StructNest

First name is: John

Last name is: Doe

Birthday is: 12/30/1978

Phone number is: 5855555

@10:00am merc-hu[48]%

8. Using functions in structure

    Code Listing: 

 

// File Name: ~ftp/pub/class/cplusplus/Structure/StrucSimple.cpp

// Purpose:   Demonstrates the use of funciton in C++ struct.

//            Stores some personal data in a structure, then prints

//            the info out.

 

#include <iostream>

using namespace std;

 

int main()

{

       

        struct PersonalData

        {

                char *FirstName;

                char *LastName;

                char *Birthday;  // in the format of 12/30/1978

                int  PhoneNum;

 

                // struc can also have member functions

                void PrintDat()

                {

                        // Print the data out

                        cout << "First name is: " << FirstName << endl;

                        cout << "Last name is: " << LastName << endl;

                        cout << "Birthday is: " << Birthday << endl;

                        cout << "Phone number is: " << PhoneNum << endl;

                }

        }; // don't forget the ending ";"

 

        // Declaring a variable of type PersonalData

        PersonalData PersonOne;

 

        // Populate PersonOne with data

        PersonOne.FirstName = "John";

        PersonOne.LastName = "Doe";

        PersonOne.Birthday = "12/30/1978";

        PersonOne.PhoneNum = 5855555;

 

        PersonOne.PrintDat();

        return 0;

}

    Running session:

@10:07am merc-hu[51]% CC -LANG:std StrucFunc.cpp -o StrucFunc

@10:08am merc-hu[52]% StrucFunc

First name is: John

Last name is: Doe

Birthday is: 12/30/1978

Phone number is: 5855555

@10:08am merc-hu[53]%

9. Pointer to derived objects

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Poly/PtrDerived.cpp

// Purpose: Using base pointers on derived class objects.

#include <iostream>

#include <cstring>

using namespace std;

 

class B_class {

  char author[80];

public:

  void put_author(char *s) { strcpy(author, s); }

  void show_author() { cout << author << "\n"; }

} ;

 

class D_class : public B_class {

  char title[80];

public:

  void put_title(char *num) {

    strcpy(title, num);

  }

  void show_title() {

    cout << "Title: ";

    cout <<  title << "\n";

  }

};

 

int main()

{

  B_class *p;

  B_class B_ob;

 

  D_class *dp;

  D_class D_ob;

 

  p = &B_ob;  // address of base

 

  // Access B_class via pointer.

  p->put_author("Tom Clancy");

 

  // Access D_class via base pointer.

  p = &D_ob;

  p->put_author("William Shakespeare");

 

  // Show that each author went into proper object.

  B_ob.show_author();

  D_ob.show_author();

  cout << "\n";

 

  /* Since put_title() and show_title() are not part

     of the base class, they are not accessible via

     the base pointer p and must be accessed either

     directly, or, as shown here, through a pointer to the

     derived type.

  */

  dp = &D_ob;

  dp->put_title("The Tempest");

  p->show_author(); // either p or dp can be used here.

  dp->show_title( );

 

  return 0;

}

 

    Running session:

@9:35am merc-hu[76]% CC -LANG:std PtrDerived.cpp -o PtrDerived

@9:35am merc-hu[77]% PtrDerived

Tom Clancy

William Shakespeare

 

William Shakespeare

Title: The Tempest

@9:35am merc-hu[78]%

10. Virtual functions and polymorphism

Code listing:

// File:    ~ftp/pub/class/cplusplus/Poly/VirtualPoly.cpp

// Purpose: Vitual function and polymorphism

 

#include <iostream>

using namespace std;

 

class figure {

protected:

  double x, y;

public:

  void set_dim(double i, double j=0) {

    x = i;

    y = j;

  }

  virtual void show_area() {

    cout << "No area computation defined ";

    cout << "for this class.\n";

  }

} ;

 

class triangle : public figure {

  public:

    void show_area() {

      cout << "Triangle with height ";

      cout << x << " and base " << y;

      cout << " has an area of ";

      cout << x * 0.5 * y << ".\n";

    }

};

 

class square : public figure {

  public:

    void show_area() {

      cout << "Square with dimensions ";

      cout << x << "x" << y;

      cout << " has an area of ";

      cout << x *  y << ".\n";

    }

};

class circle : public figure {

  public:

    void show_area() {

      cout << "Circle with radius ";

      cout << x;

      cout << " has an area of ";

      cout << 3.14 * x * x << ".\n";

    }

} ;

 

int main()

{

  figure *p; // create a pointer to base type

 

  triangle t; // create objects of derived types

  square s;

  circle c;

 

  p = &t;

  p->set_dim(10.0, 5.0);

  p->show_area();

 

  p = &s;

  p->set_dim(10.0, 5.0);

  p->show_area();

 

  p = &c;

  p->set_dim(9.0);

  p->show_area();

 

  return 0;

}

Running session:

@9:41am merc-hu[82]% CC -LANG:std VirtualPoly.cpp -o VirtualPoly

@9:41am merc-hu[83]% VirtualPoly

Triangle with height 10 and base 5 has an area of 25.

Square with dimensions 10x5 has an area of 50.

Circle with radius 9 has an area of 254.34.

@9:41am merc-hu[84]%


 

 

11. A simple program demonstrating inheritance

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/SimpleInherit.cpp

// Purpose: A simple program showing inheritance

 

#include <iostream>

using namespace std;

 

class base {

  int i, j;

public:

  void set(int a, int b) { i = a; j = b; }

  void show() { cout << i << " " << j << "\n"; }

};

 

// inheritance

class derived : public base {

  int k;

public:

  derived(int x) { k = x; }

  void showk() { cout << k << "\n"; }

};

 

int main()

{

  derived ob(3);

 

  ob.set(1, 2); // access member of base

  ob.show();    // access member of base

 

  ob.showk();   // uses member of derived class

 

  return 0;

}

    Running session:

@2:32pm merc-hu[211]% CC -LANG:std SimpleInherit.cpp -o SimpleInherit

@2:33pm merc-hu[212]% SimpleInherit

1 2

3

@2:33pm merc-hu[213]%

2. Using protected members

Code listing:

// File:    ~ftp/pub/class/cplusplus/inheritance/ProtectedMember.cpp

// Purpose: Inherite protected members of the base class

 

#include <iostream>

using namespace std;

 

class base {

protected:

  int i, j; // private to base, but accessible to derived

public:

  void set(int a, int b) { i = a; j = b; }

  void show() { cout << i << " " << j << "\n"; }

};

 

class derived : public base {

  int k;

public:

  // derived may access base's i and j

  void setk() { k = i*j; }

 

  void showk() { cout << k << "\n"; }

};

 

int main()

{

  derived ob;

 

  ob.set(2, 3); // OK, known to derived

  ob.show();    // OK, known to derived

 

  ob.setk();

  ob.showk();

 

  return 0;

}

Running session:

@10:48am merc-hu[18]% CC -LANG:std ProtectedMember.cpp -o ProtectedMember

@10:49am merc-hu[19]% ProtectedMember

2 3

6

@10:50am merc-hu[20]%

3. Protected inheritance

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/ProtectedInheritance.cpp

// Purpose: using protected for inheritance of a base class

 

#include <iostream>

using namespace std;

 

class base {

  int i;

protected:

  int j;

public:

  int k;

  void seti(int a) { i = a; }

  int geti() { return i; }

};

 

// Inherit base as protected.

class derived : protected base {

public:

  void setj(int a) { j = a; } // j is protected here

  void setk(int a) { k = a; } // k is also protected

  int getj() { return j; }

  int getk() { return k; }

};

 

int main()

{

  derived ob;

 

  /* This next line is illegal because seti() is

     a protected member of derived, which makes it

     inaccessible outside of derived. */

//  ob.seti(10);

 

//  cout << ob.geti(); // illegal -- geti() is protected

//  ob.k = 10; // also illegal because k is protected

 

  // these next statements are OK

  ob.setk(10);

  cout << ob.getk() << ' ';

  ob.setj(12);

  cout << ob.getj() << ' ';

 

  return 0;

}

 

    Running session:

@10:59am merc-hu[25]% CC -LANG:std ProtectedInheritance.cpp -o ProtectedInheritance

@10:59am merc-hu[26]% ProtectedInheritance

10 12

@10:59am merc-hu[27]%

12. Multiple inheritance 

    Code Listing: 

// File:   ~ftp/pub/class/cplusplus/Inheritance/MulInhert.cpp

// Purpose: An example of multiple base classes.

 

#include <iostream>

using namespace std;

 

class base1 {

protected:

  int x;

public:

  void showx() { cout << x << "\n"; }

};

 

class base2 {

protected:

  int y;

public:

  void showy() { cout << y << "\n"; }

};

 

// Inherit multiple base classes.

class derived: public base1, public base2 {

public:

  void set(int i, int j) { x = i; y = j; }

};

 

int main()

{

  derived ob;

 

  ob.set(10, 20); // provided by derived

  ob.showx();     // from base1

  ob.showy();     // from base2

 

  return 0;

}

 

    Running session:

@11:04am merc-hu[32]% CC -LANG:std MulInherit.cpp -o MulInherit

@11:05am merc-hu[33]% MulInherit

10

20

@11:05am merc-hu[34]%

13. Calling base class's constructor in derived class 

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/BaseConst.cpp

// Purpose: demonstrate how to call base class's constructor

 

#include <iostream>

using namespace std;

 

class base1 {

protected:

  int i;

public:

  base1(int x) { i = x; cout << "Constructing base1\n"; }

  ~base1() { cout << "Destructing base2\n"; }

};

 

class base2 {

protected:

  int k;

public:

  base2(int x) { k = x; cout << "Constructing base2\n"; }

  ~base2() { cout << "Destructing base2\n"; }

};

 

class derived: public base1, public base2 {

  int j;

public:

  derived(int x, int y, int z): base1(y), base2(z)

    { j = x; cout << "Constructing derived\n"; }

 

  ~derived() { cout << "Destructing derived\n"; }

  void show() { cout << i << " " << j << " " << k << "\n"; }

};

 

int main()

{

  derived ob(3, 4, 5);

 

  ob.show();  // displays 4 3 5

 

  return 0;

}

 

 

    Running session:

@11:11am merc-hu[35]% CC -LANG:std BaseConst.cpp -o BaseConst

@11:12am merc-hu[36]% BaseConst

Constructing base1

Constructing base2

Constructing derived

4 3 5

Destructing derived

Destructing base2

Destructing base2

@11:12am merc-hu[37]%