You are on page 1of 7

1.

Record page 110 - 115


*******************************************************
2.
// C++ Program to Copy Content of One file to Another
// ----codescracker.com-----

#include<iostream>
using namespace std;
int main()
{
char ch, sourceFile[20], targetFile[20];
FILE *fs, *ft;
cout<<"Enter the Name of Source File: ";
cin>>sourceFile;
fs = fopen(sourceFile, "r");
if(fs == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nEnter the Name of Target File: ";
cin>>targetFile;
ft = fopen(targetFile, "w");
if(ft == NULL)
{
cout<<"\nError Occurred!";
return 0;
}
ch = fgetc(fs);
while(ch != EOF)
{
fputc(ch, ft);
ch = fgetc(fs);
}
cout<<"\nFile copied successfully.";
fclose(fs);
fclose(ft);
cout<<endl;
return 0;
}
*****************************************************
3. Record page 71
#include<iostream>
using namespace std;
class f
{ int a,b;
void fun()
{
a=10,b=20;
cout<<"\nSum :: "<<(a+b);
}
public:
f()
{
a=1;
b=1;
}
friend class B;
};
class B
{
int b;
public:
void show(f & y)
{
cout<<" f::a = "<<y.a<<" f :: b = "<<y.b;
y.fun();
}
};
int main()
{ f x;
B y;
y.show(x);
}
****************************************************
4. Base and derived
//Program for student exam multiple inheritance
#include<iostream>
using namespace std;
class student
{
protected:
char name[50];
int age;
char Roll[12];
public:
void getdetails()
{
cout<<"Enter name, age, roll :: ";
cin>>name;
cin>>age;
cin>>Roll;
}
};
class exam: public student
{
protected:
int in;
int ie;
int tot;
public:
void get()
{
cout<<"Enter internal,external marks :: ";
cin>>in;
cin>>ie;
}
void display()
{
tot = ie + in;
cout<<"\nName : "<<name<<"\nAge : "<<age<<"\nROLL : "<<Roll;
cout<<"\nTotal marks :: "<<tot;
}
};

int main()
{
student S;
exam B;
B.getdetails();
B.get();
B.display();

}
***********************
5. Single level
//Program for student exam multiple inheritance
#include<iostream>
using namespace std;
class student
{
protected:
char name[50];
int age;
char Roll[12];
public:
void getdetails()
{
cout<<"Enter name, age, roll :: ";
cin>>name;
cin>>age;
cin>>Roll;
}
void display()
{
cout<<"\nName : "<<name<<"\nAge : "<<age<<"\nROLL : "<<Roll;
}
};

int main()
{
student S;
S.getdetails();
S.display();

}
6. Fibanocci using recursion
Without recursion
#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}

With recursion
#include<iostream>
using namespace std;
int n1,n2;
class print
{
public:
print()
{
n1 = 0;
n2 = 1;
}
};
void printFibonacci(int n){
static int n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
print x;
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0"<<" 1 ";
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
*******************************************************************
7. Mat multiple using DMA
#include <iostream>
#include <cassert>
using namespace std;

int** create_matrix(int rows, int cols)


{
int** mat = new int* [rows]; //Allocate rows.
for (int i = 0; i < rows; ++i)
{
mat[i] = new int[cols](); //Allocate each row and zero initialize..
}
return mat;
}

void destroy_matrix(int** &mat, int rows)


{
if (mat)
{
for (int i = 0; i < rows; ++i)
{
delete[] mat[i]; //delete each row..
}

delete[] mat; //delete the rows..


mat = nullptr;
}
}

int main()
{
int rowsA = 2; // number of rows
int colsA= 2; // number of coloumns
int i,j;
int** matA = create_matrix(rowsA, colsA);
for(int i = 0; i < rowsA; ++i)

{
for(int j = 0; j < colsA; ++j)

std::cout << "Enter element A" << i + 1 << j + 1 << " : ";
std::cin >> matA[i][j];

}
}
int rowsB = 2; // number of rows
int colsB = 2; // number of coloumns

int** matB = create_matrix(rowsB, colsB);


for(int i = 0; i < rowsB; ++i)

{
for(int j = 0; j < colsB; ++j)

std::cout << "Enter element B" << i + 1 << j + 1 << " : ";
std::cin >> matB[i][j];

}
}
//Checking matrix multiplication qualification
assert(colsA == rowsB);

int** matC = create_matrix(rowsA, colsB);

//Multiplication
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsA; ++k) //ColsA..
{
matC[i][j] += matA[i][k] * matB[k][j];

}
cout<<matC[i][j]<<' ';
}
cout<<endl;
}

//Clean up..
destroy_matrix(matA, rowsA);
destroy_matrix(matB, rowsB);
destroy_matrix(matC, rowsA);
}
*************************************************
8. Student exam
//Program for student exam multiple inheritance
//Program for student exam multiple inheritance
#include<iostream>
using namespace std;
class student
{
protected:
char name[50];
int age;
char Roll[12];
public:
void getdetails()
{
cout<<"Enter name, age, roll :: ";
cin>>name;
cin>>age;
cin>>Roll;
}
};
class internal: public student
{
protected:
int in;
int ie;
public:
void get()
{
cout<<"Enter internal,external marks :: ";
cin>>in;
cin>>ie;
}
};
class total:public internal
{
int tot;
public:
void display()
{
tot = ie + in;
cout<<"\nName : "<<name<<"\nAge : "<<age<<"\nROLL : "<<Roll;
cout<<"\nTotal marks :: "<<tot;
}

};
int main()
{
total C;
C.getdetails();
C.get();
C.display();

}
********************************************************************
9. record page 44

*******************************************************************
10. record page 99

*******************************************************************
11. record page 106

12. record page 22-26

13. record page 50

14. Book example


#include<iostream>
using namespace std;
class Book
{
protected:
char name[50];
int pages;
public:
void getdetails()
{
cout<<"Enter name, pages :: ";
cin>>name;
cin>>pages;
}
};
class libdet: public Book
{
protected:
int cost;
int sheno;
public:
void get()
{
cout<<"Enter cost and sheno";
cin>>cost;
cin>>sheno;
}
};
class res:public libdet
{
public:
void display()
{
cout<<"\nName : "<<name<<"\nPages : "<<pages;
cout<<"\ncost :: "<<cost<<"\nsheno : "<<sheno;
}

};
int main()
{
res c;
c.getdetails();
c.get();
c.display();
}

You might also like