0% found this document useful (0 votes)
10 views21 pages

Solution 6 1

The document contains multiple C++ programming tasks that demonstrate the use of classes, constructors, operator overloading, and vector manipulation. Each task includes a class definition for different entities like Rectangle, student, Complex, and Matrix, along with their respective methods and main functions to showcase their functionality. The tasks illustrate concepts such as copy constructors, operator overloading for arithmetic operations, and displaying data from vectors and matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Solution 6 1

The document contains multiple C++ programming tasks that demonstrate the use of classes, constructors, operator overloading, and vector manipulation. Each task includes a class definition for different entities like Rectangle, student, Complex, and Matrix, along with their respective methods and main functions to showcase their functionality. The tasks illustrate concepts such as copy constructors, operator overloading for arithmetic operations, and displaying data from vectors and matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab : 06

Task : 01
#include<iostream>
using namespace std;
class Rectangle{
int length;
int width;
public:
Rectangle(){
length=0;
width=0;
}
Rectangle(int l,int w){
length=l;
width=w;
}
void set(int l,int w){
length=l;
width=w;
}
Rectangle( Rectangle &r){
length=[Link];
width=[Link];
}
void display(){
cout<<"Length: "<<length<<endl;
cout<<"Width: "<<width<<endl;
}
};
int main(){
Rectangle r(56,90),r1(r);
[Link]();
[Link]();
}
Task: 02
#include<iostream>
using namespace std;
class Rectangle{
int length;
int width;
public:
Rectangle(){
length=0;
width=0;
}
Rectangle(int l,int w){
length=l;
width=w;
}
void set(int l,int w){
length=l;
width=w;
}
Rectangle( Rectangle &r){
length=[Link];
width=[Link];
}
void display(){
cout<<"Length: "<<length<<endl;
cout<<"Width: "<<width<<endl;
}
};
int main(){
Rectangle r(69,79),r1(r);
[Link]();
[Link]();
[Link](19,100);
[Link]();
}
Task: 03
#include<iostream>
#include<vector>
using namespace std;
class student{
int age;
string name;
public:
student(){
age=0;
name="";
}
student(string n,int a){
name=n;
age=a;
}
student(const student &s){
name=[Link];
age=[Link];
}
int getage() const{
return age;
}
string getname ()const{
return name;
}
void display(const vector<student> &s){
int i;
for(i=0;i<[Link]();i++){
cout<<"Name: "<<s[i].getname()<<endl;
cout<<"Age: "<<s[i].getage()<<endl;
}
}

};
int main(){
cout<<"********Original Vector*************"<<endl;
vector<student> students={
student("Ali",21),
student("sarfaraz",32)
};
student obj;
[Link](students);
cout<<"**********Copy Vector*************"<<endl;
vector<student> copy(students);
[Link](copy);
cout<<"*********Modified Copy
Vector*********"<<endl;
copy[0]={student("mahmood",78)};
[Link](copy);
}
Task:04
#include<iostream>
using namespace std;
class Complex{
int real;
int img;
public:
Complex(){
real=0;
img=0;
}
Complex(Complex &c){
real=[Link];
img=[Link];
}
Complex(int r,int i){
real=r;
img=i;
}
int getReal(){
return real;
}
int getImg(){
return img;
}
void set(int r,int i){
real=r;
img=i;
}
void display(){
if(img>=0){
cout<<real<<"+"<<img<<"i"<<endl;
}
else{
cout<<real<<img<<"i"<<endl;;
}

}
Complex operator +(Complex &c){
Complex temp;
[Link]=real+[Link];
[Link]=img+[Link];
return temp;
}
Complex operator -(Complex &c){
Complex temp;
[Link]=[Link];
[Link]=[Link];
return temp;
}
Complex operator *(Complex &c){
Complex temp;
[Link]=((real*[Link])-(img*[Link]));
[Link]=((real*[Link])+(img*[Link]));
return temp;
}

};
int main(){
Complex obj1(2,4);
Complex obj2(2,4);
Complex obj3;
cout<<"*************Adding
Operator**************"<<endl;
obj3=obj1+obj2;
[Link]();
cout<<"*************substracting
Operator**************"<<endl;
[Link](4,-7);
[Link](2,4);
obj3=obj1-obj2;
[Link]();
cout<<"*************Multiplying
Operator**************"<<endl;
[Link](2,3);
[Link](4,5);
obj3=obj1*obj2;
[Link]();
}
Task: 05
#include <iostream>
using namespace std;

class Matrix {
int row;
int column;
int arr[100][100];
int i,j;
public:
Matrix() {
row = 0;
column = 0;
}

Matrix(int r,int c) {
row = r;
column = c;
}

Matrix(const Matrix &m) {


row = [Link];
column = [Link];
for (i=0;i<row;i++) {
for (j=0;j<column;j++) {
arr[i][j] = [Link][i][j];
}
}
}

void getElement() {
for (i=0;i<row;i++) {
cout << "Enter " << i + 1 << " Row Element: " << endl;
for (j=0;j<column;j++) {
cin >> arr[i][j];
}
}
}

void displayElement() const {


for (int i=0;i<row;i++) {
for (int j=0;j<column;j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout<<"***********************"<<endl;
}

Matrix operator+(const Matrix &m) {

Matrix temp(row, column);


for (i=0;i<row;i++) {
for (j=0;j<column;j++) {
[Link][i][j] = arr[i][j] + [Link][i][j];
}
}
return temp;
}

Matrix operator-(const Matrix &m) {


Matrix temp(row,column);
for (i=0;i<row;i++) {
for (j=0;j<column;j++) {
[Link][i][j] = arr[i][j] - [Link][i][j];
}
}

return temp;
}
Matrix operator *(const Matrix &m) {
Matrix temp(row,column);
for (i=0;i<row;i++) {
for (j=0;j<column;j++) {
[Link][i][j] = arr[i][j] * [Link][i][j];
}
}

return temp;
}

};

int main() {
Matrix m(2, 2), m1(2, 2), m2;
[Link]();
[Link]();
cout<<"****Martrix****"<<endl;
[Link]();
[Link]();
cout << "****After Adding Matrix****" << endl;
m2 = m + m1;
[Link]();
cout << "****After Substract Matrix****" << endl;
m2 = m-m1;
[Link]();
cout << "****After Multiply Matrix****" << endl;
m2 = m*m1;
[Link]();

return 0;
}
Task: 06

You might also like