University Institute of Engineering and Technology, Panjab University, Hoshiarpur
University Institute of Engineering and Technology, Panjab University, Hoshiarpur
C++ ASSIGNMENT
AMIT 1
c2. WAP to
assign the
public object
member
address to a
pointer
3. WAP using
class to
implement a
shopping list
for
departmental
store.
4. WAP to
implement
basic
operations
i.e setw,
setprecision
5. WAP to
swap two
numbers
6. WAP to
design a
student class
(inheritance
and virtual
class
concept)
7. WAP to
display all
the prime
numbers
from 1 to n.
8. WAP to
access the
members of
student class
using
pointer to
object
member
9. WAP to
check
number is
palindrome
or not. AMIT 2
10. WAP to
CONTENT
AMIT 3
ACKNOWLEDEMENT
I would like to express my special thanks all my friends for their time and efforts
they provided throughout this project. I would like to acknowledge that this project
was completed entirely by me .
AMIT KUMAR
AMIT 4
Program-1
WAP in C++ to illustrate the concept of class having an object.
#include<iostream>
using namespace std;
class demo{
int m;
public: ,n., S
void display(){
cout<<"object created"<<endl;
}
};
int main(){
demo d1;
d1.display();
}
OUTPUT:
Program-2
AMIT 5
WAP in C++ to assign the public object member address to a pointer
#include<iostream>
using namespace std;
class Demo{
public:
int a=20;
};
int main(){
system("cls");
Demo *p = new Demo;
cout<<"Number is: "<<p->a<<"\n";
return 0;
}
OUTPUT:
AMIT 6
Program-3
WAP using class to process a shopping list for a departmental store. The list
includes the details such as code number and price of each item and perform
operations like adding an item ,deleting an item and printing the whole total.
#include<iostream>
using namespace std;
class item{
int setCode[10];
int setPrice[10];
int count;
public:
void setCount(){
count=0;
}
void addItem(){
count++;
cout<<"enter the code"<<endl;
cin>>setCode[count];
}
void deleteItem(){
int a;
cout<<"enter the code of item you want to delete"<<endl;
AMIT 7
cin>>a;
for(int i=0 ; i<10 ; i++)
{
if(a==setCode[i])
setPrice[i]=0;
}
}
void displayItems(){
cout<<"*********Displaying the items**********"<<endl;
if(setCode[0]!=1)
cout<<"*******List is empty********"<<endl;
else
{
for(int i=0 ; i<10 ; i++)
{
cout<<"Item code: "<<setCode[i]<<" "<<"Item price: "<<setPrice[i]<<endl;
}
}
}
void Totalprice(){
int sum =0;
for(int i=0 ; i<10 ; i++)
{
sum=sum+setPrice[i];
}
cout<<"total price is "<<sum<<endl;
}
AMIT 8
};
int main()
{
int c;
item i1;
i1.setCount();
do
{
cout<<"choose the options below"<<endl;
cout<<"1.add items"<<endl
<<"2.delete items"<<endl
<<"3.display items"<<endl
<<"4.total price of all items"<<endl
<<"5.exit"<<endl;
cin>>c;
switch(c)
{
case 1:
i1.addItem();
break;
case 2:
i1.deleteItem();
break;
case 3:
i1.displayItems();
break;
case 4:
i1.Totalprice();
AMIT 9
break;
}
} while (c!=5);
return 0;
}
OUTPUT:
AMIT 10
Program-4
WAP implementing basic operations of class i.e setw,setprecision.
#include<iostream>
#include<iomanip>
using namespace std;
class manipulation{
int a;
double b;
public:
manipulation(int x, double y){
a=x;
b=y;
}
void displaySetprecision() {
cout<<setprecision(5)<<b<<endl;
cout<<fixed<<setprecision(2)<<b<<endl;
}
void setwidth(){
cout<<setw(10)<<a<<endl;
}
};
int main (){
manipulation m1(1234,2345.456);
m1.displaySetprecision();
m1.setwidth();
return 0;
}
AMIT 11
OUTPUT:
AMIT 12
Program-5
WAP to swap two numbers
#include<iostream>
using namespace std;
class Swap
{
int a,b;
public:
Swap()
{}
Swap(int x,int y){
a=x;
b=y;
}
void SwapNumbers(){
int temp;
temp=a;
a=b;
b=temp;
}
void display(){
cout<<a<<" "<<b;
}
};
int main()
AMIT 13
{
int a,b;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
cout<<"\nthe values of a and b before swap are: "<<endl;
Swap s1(a,b);
s1.display();
s1.SwapNumbers();
cout<<"\nthe values of a and b after swap are: "<<endl;
s1.display();
return 0;
}
OUTPUT:
AMIT 14
Program-6
WAP to design a student class representing student roll no. and a test class
representing students score in various subjects and sport class representing the
score in sports the sport and test class should be inherited from a result class
having the functionality to add score and display final result
#include<iostream>
using namespace std;
class Student
{
protected:
int roll;
};
AMIT 15
int score;
Sports(int s):score(s) {}
void show()
{
cout<<"Score = "<<score<<endl;
}
};
int main(){
Result *r = new Result(4,90,95,75);
r->show();
return 0;
}
OUTPUT
AMIT 16
AMIT 17
Program-7
WAP to generate all the prime numbers between 1 to n.
#include<iostream>
using namespace std;
int main() {
int n;
cout<<"Enter the number to print prime numbers upto that number"<<endl;
cin>>n;
for(int i=2 ; i<=n ; i++){
int count =0;
for(int j=2 ; j<=i/2 ; j++){
if (i%j==0)
count++;
}
if(count ==0 )
cout<<i<<" ";
}
return 0;
}
OUTPUT
AMIT 18
Program-8
WAP to access the members of student class using pointer to object member
#include <iostream>
using namespace std;
class student{
int n1;
public:
student(){
n1 = 0;
}
void setn(int m){
n1 = m;
}
void show(){
cout << "Number entered is " << n1;
}
};
int main(){
int x;
student o;
student *p = &o;
cout<<"enter the number"<<endl;
cin>>x;
p->setn(x);
p->show();
return 0;}
AMIT 19
OUTPUT
AMIT 20
Program-9
WAP to check number is palindrome or not.
#include<iostream>
using namespace std;
int main(){
int x, a, t1, s = 0;
cout<<"Enter n: ";
cin>>x;
t1 = x;
while (x > 0) {
a = x % 10;
s = (s * 10) + a;
x = x / 10; }
if (t1 == s){
cout<<t1<<" is a Palindrome."<<endl; }
else {
cout<<t1<<" is a not Palindrome."<<endl; }
return 0;
}
OUTPUT
AMIT 21
Program-10
WAP to implement friend function.
#include<iostream>
using namespace std;
class calculator{
int a, b, s;
public:
void input()
{
cout<<"Enter two Numbers: ";
cin>>a>>b;
}
friend void sum(calculator);
};
void sum(calculator c)
{
c.s = c.a + c.b;
cout<<"Sum of "<<c.a<<" and "<<c.b<<" = "<<c.s<<endl;
}
int main()
{
calculator c1;
c1.input();
sum(c1);
return 0;
}
AMIT 22
OUTPUT
AMIT 23
Program-11
WAP to find the factorial of given number.
#include<iostream>
using namespace std;
int factorial(int n){
if(n==1 || n== 0 )
return 1;
else
return n*factorial(n-1);
}
int main(){
int n;
cout <<"Enter the number to find its factorial"<<endl;
cin>>n;
cout<<"The factorial of "<<n<<" "<<"is "<<factorial(n);
return 0;}
OUTPUT
AMIT 24
Program-12
WAP to implement STL.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n, ch, br = 1;
cout << "Enter number of items: ";
cin >> n;
string s, s1;
vector<string> v;
for (int i = 0; i < n; i++) {
cin >> s;
v.push_back(s);
}
do {
cout << "1.deletion"<<endl<<"2.insertion"<<endl<<"3. printing"<<endl;
cin >> ch;
switch (ch){
case 1:
{
cout << "Enter item to be deleted: ";
cin >> s1;
for (auto itr = v.begin(); itr != v.end();{
if (*itr == s1) {
v.erase(itr++);
AMIT 25
}
else
++itr;
}
break;
}
case 2:
{
int pos, ct = 0;
cout << "Enter item to be inserted: ";
cin >> s1;
cout << "Enter location to be inserted: ";
cin >> pos;
for (auto itr = v.begin(); itr != v.end();){
if (ct == pos){
v.insert(itr++, s1); }
else
++itr;
ct++; }
break;
}
case 3:
{
cout << "Vector Elements: \n";
for (auto &ele : v)
{
cout << ele << " ";
}
AMIT 26
cout << "\n";
break;
}
default:
{
cout << "Wrong choice\n";
break;
}
}
cout << "Enter 0 to terminate: ";
cin >> br;
} while (br != 0);
return 0;
}
OUTPUT
AMIT 27
Project
Tic-tac-toe Game
Introduction:
Tic-tac-toe is a simple, two-player game that, if played optimally by both players, will always
result in a tie. The game is also called noughts and crosses or Xs and Os.
Tic-tac-toe is a game that is traditionally played by being drawn on paper, and it can be played
on a computer or on a variety of media.
The goal of tic-tac-toe is to be the first player to get three in a row on a 3-by-3 grid or four in a
row in a 4-by-4 grid.
To start, one player draws a board, creating a grid of squares, usually 3-by-3 or 4-by-4.
In a 3-by-3 grid game, the player who is playing "X" always goes first. Players alternate placing
Xs and Os on the board until either player has three in a row, horizontally, vertically, or
diagonally or until all squares on the grid are filled. If a player is able to draw three Xs or three
Os in a row, then that player wins. If all squares are filled and neither player has made a
complete row of Xs or Os, then the game is a draw.
One of the game's best strategies involves creating a "fork," which is placing your mark in such a
way that you have the opportunity to win two ways on your next turn. Your opponent can only
block one, thereby, you can win after that.
The gameplay is the same if you are playing on a 4-by-4 grid. The "X" player goes first. And,
players alternate placing Xs and Os on the board until a row is completed horizontally, vertically,
or diagonally, or all 16 squares are filled. If all 16 squares are filled and neither player has four in
a row, the game is a draw.
Code:
#include <iostream>
using namespace std;
char layout[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
char current_XO;
int current_player;
AMIT 28
void makeLayout(){
cout << endl;
cout << " " << layout[0][0] << " "
<< "|"
<< " " << layout[0][1] << " "
<< "|"
<< " " << layout[0][2] << endl;
cout << "-------------" << endl;
cout << " " << layout[1][0] << " "
<< "|"
<< " " << layout[1][1] << " "
<< "|"
<< " " << layout[1][2] << endl;
cout << "-------------" << endl;
cout << " " << layout[2][0] << " "
<< "|"
<< " " << layout[2][1] << " "
<< "|"
<< " " << layout[2][2] << endl;
cout << endl;
}
AMIT 29
row = placeholder / 3;
// cout<<row<<endl;
int col;
if (placeholder % 3 == 0)
col = 2;
else
col = (placeholder % 3) - 1;
// cout<<col<<endl;
layout[row][col] = current_XO;
}
int winner(){
for (int i = 0; i < 3; i++){
if (layout[0][i] == layout[1][i] && layout[1][i] == layout[2][i])
return current_player;
else if (layout[i][0] == layout[i][1] && layout[i][1] == layout[i][2])
return current_player;
}
if (layout[0][0] == layout[1][1] && layout[1][1] == layout[2][2])
return current_player;
else if (layout[0][2] == layout[1][1] && layout[1][1] == layout[2][0])
return current_player;
return 0;
}
void swap_playerAndMarker(){
if(current_XO =='X')
current_XO='O';
else
AMIT 30
current_XO='X';
if(current_player==1)
current_player=2;
else
current_player=1;
}
void game(){
cout<<"Player 1 ,choose your marker:";
char marker_p1;
cin>>marker_p1;
current_player=1;
current_XO = marker_p1;
cout<<endl;
makeLayout();
int slot;
int playerWon;
for(int i=0 ; i<9 ; i++){
cout<<endl<<"Player "<<current_player<<" it's your turn enter your slot: ";
cin>>slot;
decidingRowColumn(slot);
makeLayout();
playerWon= winner();
if(playerWon==1){
cout<<"Player 1 is winner, Congratulations!"<<endl;
break;
}
AMIT 31
else if(playerWon==2) {
cout<<"Player 2 is winner, Congratulations!"<<endl;
break;
}
swap_playerAndMarker();
}
if(playerWon==0){
cout<<"It's a tie!"<<endl;
}
}
int main()
{
cout << endl;
cout << "***********Welcome to Tic Tac Toe Game*************" << endl<<endl;
game();
}
AMIT 32
OUTPUT:
AMIT 33