Friday, 30 March 2018

Object Oriented Programming: No. 11


No: 11


1. Experiment vision: Write A C++ Program That Counts Number Of Words In A File.
2. OBJECTIVE: Program to implement counting number of words in a File
3. THEORY: In this prgram, we will find the total number of words in a file. For this we used file related manipulation functions. Initially we declare the fstream.h which is file related stream file in as a header file into our program. First we open the text file from which we have to count the number of words, using fopen() function. Then using seekg pointer we move the pointer from staring and counting the number of words till the end of file appears.
4. PROGRAM:
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
int count=1;
ifstream f1;
f1.open("student.txt");
f1.seekg(0);
char ch;
while(! f1.eof())
{
f1.get(ch);
}
if(ch==' '|| ch=='\t'|| ch=='\n')
{
count++;

}
cout<<"Number of words="<<count;
f1.close();
return 0;
}

  1. OUTPUT:


6. CONCLUSION: Thus using C++ program we have successfully executed the program for counting number of words.
7. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the experiment, and check the algorithm and program.
7. REMARK:
A template can be considered as a macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type.
8. Discussion Questions:
1) What is EOF?.
____________________________________________________________________________________________________________________________________________________________
2) What is the use of get()?
____________________________________________________________________________________________________________________________________________________________



Monday, 19 March 2018

Object Oriented Programming: No. 12


No: 12


1. Experiment vision: Write a C++ program that creates an output file, writes information to it; closes the file and open it again as an input file and read the information from the file.
2. OBJECTIVE: Program to implement File related program, to create a file and writing operation will be done after that close the file. Again open the same file in reading mode.
3. THEORY: So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. Now we will see how to read and write from a file. This requires another standard C++ library called fstream.
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.
fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
Files/streams are the set of characters, using C++ programs, we will learn to write, read data in, from the files (both text and binary).
4. PROGRAM:
#include<fstream>
#include<iostream>
using namespace std;
class student
{ public:
char name[20];
int roll_no;
void getdata()
{ cout<<"enter name";
cin>>name;
cout<<"enter roll no";
cin>>roll_no;
}
void putdata()
{ cout<<"name:"<<name;
cout<<"roll is"<<roll_no;
}
};
int main()
{
student stud, stud1;
stud.getdata();
ofstream of("data.txt");
of.write((char *) &stud, sizeof(stud)); //writing object…
of.close();
ifstream f("data.txt");
f.read((char *) &stud1, sizeof(stud1)); //reading object..
stud1.putdata();
return 0;
}
5. OUTPUT:










  1. CONCLUSION: Thus using C++ program we have successfully executed the program for file related operations.
7. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the experiment, and check the algorithm and program.


7. REMARK:
A file related basic operations such as create a new file, writing data into a file, close a file. Again open a file in reading mode and close the file.
8. Discussion Questions:
1) Explain File in detail.
____________________________________________________________________________________________________________________________________________________________
2) What are the types of file opening?
____________________________________________________________________________________________________________________________________________________________
3) What is EOF?
____________________________________________________________________________________________________________________________________________________________


Object Oriented Programming: No. 10


 No: 10


1. Experiment vision: To write a program on the concept of Template function.
2. OBJECTIVE: Program to implement Template function.
3.1 THEORY:
  • Template
The template in C++ allows us to operate on any data type for which the internal implementation is appropriate. Template can be defined for both classes and functions therefore there are two types of template in C++.
1)Function Template
2)Class Template
  • Function Template:- Function Template could be used to create a family of functions with different argument types.
  • Class Template:- Class Template are generally used for data storage classes.
3.2 PROGRAM:
1) Following is an example which makes use of Function template.
#include <iostream>
using namespace std;
template <class T>
void swap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"m and n before swap:"<<m<<" "<<n<<"\n";
swap(m,n);
cout<<"m and n after swap:"<<m<<" "<<n<<"\n";
cout<<"a and b before swap:"<<a<<" "<<b<<"\n";
swap(a,b);
cout<<"a and b after swap:"<<a<<" "<<b<<"\n";
}
int main()
{
fun(100,200,11.22,33.44);
return 0;
}
3.3 OUTPUT:
m and n before swap: 100 200
m and n after swap: 200 100
a and b before swap: 11.22 33.44
a and b after swap: 33.44 11.22
4.1 PROGRAM:
2) Following is an example which makes use of Class template.
#include <iostream>
using namespace std;
template<class t1,class t2>
class test
{
t1 a;
t2 b;
public:
test(t1 x,t2 y)
{
a=x;
b=y;
}
void show()
{
cout<<a<<"and"<<b<<"\n";
}
};
int main()
{
test<float,int>test1(1.23,123);
test<int,char>test2(100,'w');
test1.show();
test2.show();
return 0;
}
4.2 OUTPUT:
5. conclusion:
Thus C++ programs by using template function have been executed successfully.
6. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.
7. REMARK:
A template can be considered as a macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type.
8. Discussion Questions:
1) Explain Template in detail.
____________________________________________________________________________________________________________________________________________________________
2) What are the two types of template?
____________________________________________________________________________________________________________________________________________________________
3) What is syntax for writing class template?
____________________________________________________________________________________________________________________________________________________________
4) What is syntax for writing function template?
____________________________________________________________________________________________________________________________________________________________
5) Explain class template with multiple argument.



About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.