class,create text file using c++ - learnit

Home Top Ad

Post Top Ad

Thursday, May 21, 2020

class,create text file using c++

C Programming How to Use switch Statement

What is C++ Programming

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.


Why Learn C++ Programming

-C++ is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance.

-After learning C++, it will be much easier to learn other programming languages like Java, Python, etc.

-C++ helps you to understand the internal architecture of a computer, how computer stores and retrieves information.


C++ Classes and Objects

Class: A class in C++ is the building block that leads to Object-Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.


A Class is a user-defined data-type that has data members and member functions.

Data members are the data variables, member functions are the functions used to manipulate these variables, and together these data members and member functions defines the properties and behavior of the objects in a Class.


C++ program to create a file

Problem Statement: Write a C++ program to create a file using file handling and check whether the file is created successfully or not. If a file is created successfully then it should print “File Created Successfully” otherwise should print some error message.


Approach: Declare a stream class file and open that text file in writing mode. If the file is not present then it creates a new text file. Now check if the file does not exist or not created then return false otherwise return true.


In the above example of class MyTest

#include<iostream>
#include<fstream>
using namespace std;
class mytest{
public: void writedata)
{
int total,kw,price;
cout<<"Please input Kw";
cin>>kw;
cout<<Please input Price";
cin>>price;
total=kw*price;
ofstream write("customer.txt");
write<<total;
write.close();
}
public: void readdata()
{
string st;
ifstream readdatakw("customer.txt");
while(getline(readdatakw,st))
{
cout<<st;
}
}
};
int main()
{
mytest obj;
int j;
cout<<"Please Choose Option is Below"<<endl;
cout<<"1: Input Data";
cout<<"2: read Data";
cin>>j;
switch(j)
{
case 1:
obj.writedata();
break;
case 2:
obj.readdata();
break;
deafult:
cout<<"Choose Option 1 or 2";
break;
}
}

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad