lab7
task 1:
#include <iostream>
using namespace std;
class Point
{public:
Point();
Point( int x1, int y1 );
friend istream& operator >>( istream& in, Point& p);
friend ostream& operator <<( ostream& os, const Point& p);
private:
int x;
int y;};
ostream& operator<<(ostream& os, const Point& p)
{os<<"("<<p.x<<","<<p.y<<")"<<endl;
return os;}
istream& operator>>(istream& is, Point& p)
{is>>p.x>>p.y;
return is;}
Point::Point()
{x=0;
y=0;}
Point::Point(int x1, int y1 )
{x=x1;
y=y1;}
int main()
{Point x(4,5);
Point y;
cout<<"testing overloaded << operator\n";
cout<<"x="<<x;
cout<<"y="<<y;
cout<<"testing overloaded >> operator, enter a point in the
format x y :\n";
cin>>y;
cout<<"y="<<y;
return 0;}
Task 2:
#ifndef STUDENT_H
#define STUDENT_H
class cStudent {
private:
int mark;
public:
cStudent();
void mSetMark(int);
int mGetMark();
};
#endif
#include <iostream>
using namespace std;
cStudent::cStudent() : mark() {}
void cStudent::mSetMark( int m ) {
mark = m;
}
int cStudent::mGetMark() {
return mark;
}
#include <iostream>
#include <ctime>
using namespace std;
int main () {
// marks will be determined by chance -->
srand((unsigned)time(NULL));
cStudent student[10];
int sumMarks=0;
cout << "Marks students : " << endl;
for ( int i=0; i < 10; i++ ) {
student[i].mSetMark(rand()%12+1);
cout << i+1 << "student : mark - " <<
student[i].mGetMark() << endl;
}
for ( int i=0; i < 10; i++ )
sumMarks += student[i].mGetMark();
cout << "------------------------------------\n"
<< "sumMarks = " << sumMarks << endl;
return 0;
}
task 3:
#ifndef STUDENT_H
#define STUDENT_H
class cData {
private:
double time[5]; //array of time in second
int distance; //distance in meter
double min, max, average;
void mStatistics();
public:
cData();
void mSetDistance(int);
void mSetTimes(int,double);
void mDisplay();
};
#endif
#include <iostream>
using namespace std;
int main () {
int dis;
double time;
cData data;
cout << "enter distance :
";
cin >> dis;
data.mSetDistance(dis);
cout << "enter time
: " << endl;
for (int i=0; i < 5; i++ ) {
cout << i+1 << " time - ";
cin >> time;
data.mSetTimes(i,time);
}
cout << "----------------------------\n";
data.mDisplay();
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
cData::cData() : distance(), min(), max(), average(0) {
memset(time,0.0,5);
}
void cData::mSetDistance( int d ) {
distance = d;
}
void cData::mSetTimes( int i, double t ) {
time[i] = t;
}
void cData::mDisplay() {
mStatistics();
cout << "distance - "
<< "all time :
for ( int i=0; i < 5;
cout << time[i]
cout << endl;
cout << "min time
cout << "max time
cout << "average time
}
<< distance << endl
";
i++ )
<< ' ';
:
:
:
" << min << endl;
" << max << endl;
" << average << endl;
void cData::mStatistics() {
average = min = max = time[0];
for ( int i=1; i < 5; i++ ) {
average += time[i];
if ( min > time[i] )
min = time[i];
if ( max < time[i] )
max = time[i];
}
average/=5;
}
Task 4:
1. #include <iostream>
using namespace std;
class CharClass
{
private:
char ch;
public:
CharClass(char c);
char get_ch();
};
CharClass::CharClass(char c)
{ch =c;}
char CharClass::get_ch()
{return ch;}
int main()
{
CharClass ob[10] = { 'a', 'b', 'c','d', 'e', 'f', 'g', 'h', 'i', 'j'
};
int i;
for(i = 0; i <10; i++)
cout << ob[ i ].get_ch() << ' ';
cout << endl;
return 0;
}
2.#include <iostream>
using namespace std;
class MyClass {
private:
int i;
public:
void setInt(int j)
{ i=j; }
int getInt()
{ return i; }};
int main()
{MyClass myObject[3];
int i;
for(i=0; i<3; i++)
myObject[i].setInt(i+1);
for(i=0; i<3; i++)
cout << myObject[i].getInt() << "\n";
return 0;}
3.#include <iostream>
using namespace std;
class MyClass
{
public:
int m_Number;
char m_Character;
};
void main()
{
MyClass *pPointer;
pPointer = new MyClass;
pPointer->m_Number = 10;
pPointer->m_Character = 's';
delete pPointer;
}
Task 5:
#include<iostream>
using namespace std;
class rectangle
{
friend ostream& operator<<(ostream & os, const rectangle& r);
friend istream& operator>>(istream& is, rectangle& r);
friend rectangle Multiply (const rectangle& i, const rectangle& r);
private:
int x;
int y;
public:
rectangle();
rectangle(int a, int b);
int Getx()const;
int Gety()const;
void Setx(int a);
void Sety(int b);
};
int rectangle::Getx()const
{
return (x);
}
int rectangle::Gety()const
{
return (y);
}
void rectangle::Setx(int a)
{
x=a;
}
void rectangle::Sety(int b)
{
y=b;
}
istream& operator>>(istream& is , rectangle& r )
{
char m;
is>>r.x;
is>>m;
return is;
}
ostream& operator<<(ostream& os, const rectangle& r)
{
os<<r.x<<" Meter";
return os;
}
rectangle Multiply (const rectangle& r, const rectangle& i)
{
rectangle c ;
c.x = (r.x*i.x);
return c;
}
rectangle::rectangle()
{
x=0;
y=0;
}
rectangle::rectangle(int a, int b )
{
x=a;
y=b;
}
int main()
{
rectangle a;
rectangle b;
cout<<"Enter the length of a rectangle in meter:"<<endl;
cin>>a;
cout<<"Enter the width of a rectangle in meter:"<<endl;
cin>>b;
cout<<"You entered "<<a<<" of length and "<<b<<" of widtg"<<endl;
rectangle c = Multiply(a,b);
cout<<"the area = "<<c<<endl;
return 0;
}
© Copyright 2026 Paperzz