C++ Assignment 11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Q1- What is the const keyword in C++?

Show at least 5 uses of


const keyword in C++ with proper example?

Ans: - Const keyword is used to make any element of a program


constant. If we declare a variable as const, we cannot change its value. A
const variable must be assigned a value at the time of its declaration.
Once initialized, if we try to change its value, then we will get
compilation error. The following two cases will give us an error since in
both these cases, we are trying to change the value of a const variable.
Here are the five uses of constant keyword in C++ along with example: -

a).The const keyword specifies that a variable's value is constant and


tells the compiler to prevent the programmer from modifying it.
Foe example: -
#include <iostream>
using namespace std;
int main()
{
const int SIDE = 50;
int area;
area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<" is: "
<< area << endl;
system("PAUSE");
return 0;
}

b). The const keyword can also be used in pointer declarations.


For example: -
int main()
{
char *mybuf = 0, *yourbuf;
char *const aptr = mybuf;
*aptr = 'a';
aptr = yourbuf;
}

c).We can use pointers to constant data as function parameters to


prevent the function from modifying a parameter passed through a
pointer.

d). Declaring a member function with the const keyword


specifies that the function is a "read-only" function that does not
modify the object for which it is called. A constant member
function cannot modify any non-static data members or call any
member functions that aren't constant. For example: -
#include<iostream>
Using namespace std;
class Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const;
void setMonth( int mn );
private:
int month;
};
int Date::getMonth() const
{
return month;
}
void Date::setMonth( int mn )
{
month = mn;
}
int main()
{
Date MyDate( 7, 4, 1998 );
const Date BirthDate( 1, 18, 1953 );
MyDate.setMonth( 4 );
BirthDate.getMonth();
BirthDate.setMonth( 4 );
}

e). We can also overload a member function using the const


keyword; this allows a different version of the function to be called
for constant and nonconstant objects.

Q2- Explain various types of mode in C++ used for file handling
with proper code?

Ans: - Different file opening modes in C++ are as follows:


i) ios::in: - Opens a file for reading. New contents cannot be
written in the file.
ii) ios::out: - Opens a file for writing. If file exist already then, its
contents are deleted but if the file does not exist it will create a new
file. Program related to ios::in & ios:: out is given below: -
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream newfile;
newfile.open("harsh.txt",ios::out);
if(newfile.is_open())
{
newfile<<"Hello, I'm Harsh Singh. \n";
newfile.close();
}
newfile.open("harsh.txt",ios::in);
if (newfile.is_open())
{
string tp;
while(getline(newfile, tp))
{
cout << tp << "\n";
}
newfile.close();
}
}

iii) ios::binary: - It opens a file in binary form. Program related to


this mode is as follow: -
struct Person
{
char name[50];
int age;
char phone[24];
};
int main()
{
Person me = {"ABC", 18, "364-2534"};
Person book[30];
int x = 123;
double fx = 34.54;
ofstream outfile;
outfile.open("junk.dat", ios::binary | ios::out);
outfile.write(&x, sizeof(int)); // sizeof can take a type
outfile.write(&fx, sizeof(fx)); // or it can take a variable name
outfile.write(&me, sizeof(me));
outfile.write(book, 30*sizeof(Person))
outfile.close();
}

iv) ios::truncate: - Opens a file for writing. If file already exists then
its previous contents are replaced with the new contents.
v) ios::app: - Opens a file in append mode. It adds new content at the
end of the file. Program related to this mode is as follow: -
#include<fstream>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class student
{
int rollno;
char name[20];
char branch[3];
float marks;
char grade;
public:
void getdata()
{
cout<<"Rollno: ";
cin>>rollno;
cout<<"Name: ";
cin>>name;
cout<<"Branch: ";
cin>>branch;
cout<<"Marks: ";
cin>>marks;
if(marks>=75)
{
grade = 'A';
}
else if(marks>=60)
{
grade = 'B';
}
else if(marks>=50)
{
grade = 'C';
}
else if(marks>=40)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
void putdata()
{
cout<<name<<", rollno "<<rollno<<" has ";
cout<<marks<<"% marks and "<<grade<<"
grade."<<"\n";
}
int getrno()
{
return rollno;
}
}stud1;
int main()
{
student obj;
system("cls");
ofstream fout("marks.dat", ios::app);
char ans='y';
while(ans=='y' || ans=='Y')
{
stud1.getdata();
fout.write((char *)&stud1, sizeof(stud1));
cout<<"Data appended in the file successfully..!!\n";
cout<<"\nWant to enter more ? (y/n)..";
cin>>ans;
}
obj.putdata();
fout.close();
cout<<"\nPress any key to exit.\n";
getch();
}

vi) ios::ate” - It is used to write new contents at the end of the file and
we can also modify the previous contents.
vii) ios::nocreate: - It does not allow to create a new file if it does not
exist.
viii) ios::noreplace: - It does replace the old file with new file.

Q3- Write a program in C++ to create three string objects with


values: KING, MARTIN, LUTHER respectively. Now concatenate
them into one string object as KINGMARTINLUTHER using
(i). append function
(ii). + operator

Ans: - Using append function: -


#include <iostream>
#include <string>
using namespace std;
int main()
{
string data1 = "KING";
string data2 = "MARTIN";
string data3 = "LUTHER";
string data4 = data1.append(data2).append(data3);
cout << data4;
return 0;
}
Using + operator: -
#include <iostream>
#include <string>
using namespace std;
int main()
{
string data1 = "KING";
string data2 = "MARTIN";
string data3 = "LUTHER";
string data4 = data1 + data2 + data3;
cout << data4;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy