Results 1 to 3 of 3

Thread: C++ Quick Guide Class 4

  1. #1
    Join Date
    28 Apr 2015
    Location
    Sindh
    Gender
    Male
    Posts
    386
    Threads
    81
    Credits
    1,802
    Thanked
    38

    Post C++ Quick Guide Class 4


    Asslam-0-Alaikum,
    Guys This is 4th Class Of C++ Quick Guide,

    So Start Now:
    Data Encapsulation in C++:
    All C++ programs are composed of following two fundamental elements:

    * Program statements (code): This is the part of a program that performs actions and they are called functions.

    * Program data: The data is the information of the program which affected by the program functions.

    Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

    C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example:
    Code:
    class Box
    {
       public:
          double getVolume(void)
          {
             return length * breadth * height;
          }
       private:
          double length;      // Length of a box
          double breadth;     // Breadth of a box
          double height;      // Height of a box
    };

    C++ Files and Streams:

    The iostream standard library cin and cout methods for reading from standard input and writing to standard output respectively.

    To read and write from a file requires another standard C++ library called fstream which defines three new data types:
    Data Type Description
    ofstream This data type represents the output file stream and is used to create files and to write information to files.
    ifstream This data type represents the input file stream and is used to read information from files.

    Following is the C++ program, which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
     
    int main ()
    {
        
       char data[100];
     
       // open a file in write mode.
       ofstream outfile;
       outfile.open("afile.dat");
     
       cout << "Writing to the file" << endl;
       cout << "Enter your name: "; 
       cin.getline(data, 100);
     
       // write inputted data into the file.
       outfile << data << endl;
     
       cout << "Enter your age: "; 
       cin >> data;
       cin.ignore();
       
       // again write inputted data into the file.
       outfile << data << endl;
     
       // close the opened file.
       outfile.close();
     
       // open a file in read mode.
       ifstream infile; 
       infile.open("afile.dat"); 
     
       cout << "Reading from the file" << endl; 
       infile >> data; 
     
       // write the data at the screen.
       cout << data << endl;
       
       // again read the data from the file and display it.
       infile >> data; 
       cout << data << endl; 
     
       // close the opened file.
       infile.close();
     
       return 0;
    }

    Guysss Wait 4 Nexxt ClaSSSSSSS.

    Thanku ALLAH HaFiZ...........



  2. #2
    Irfan596 is offline Senior Member
    Last Online
    14th February 2019 @ 03:19 AM
    Join Date
    10 Jan 2014
    Age
    25
    Gender
    Male
    Posts
    1,035
    Threads
    98
    Credits
    128
    Thanked
    35

    Default

    Nice
    AnY TimEs MeaN' No TiMe.

  3. #3
    Join Date
    28 Apr 2015
    Location
    Sindh
    Gender
    Male
    Posts
    386
    Threads
    81
    Credits
    1,802
    Thanked
    38

    Default

    Shokriya

Similar Threads

  1. C++ Quick Guide Class 2
    By A_Qayoom in forum C++
    Replies: 3
    Last Post: 22nd March 2018, 10:52 PM
  2. C++ Quick Guide Class 1
    By A_Qayoom in forum C++
    Replies: 12
    Last Post: 22nd March 2018, 10:51 PM
  3. C++ Quick Guide Class 3
    By A_Qayoom in forum C++
    Replies: 0
    Last Post: 9th January 2016, 03:41 PM
  4. Replies: 28
    Last Post: 1st September 2015, 08:29 PM
  5. Replies: 2
    Last Post: 31st May 2015, 06:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •