using namespace std;
class student{
string sname;int id ,totalmarks;
public:
void getdata()
{
cout<<"Enter Name Of Student :";
cin>>sname;
cout<<"Enter Student ID :";
cin>>id;
cout<<"Enter the Total maeks out of 500 :";
cin>>totalmarks;
}
void showdata()
{
cout<<"Your Name :"<<sname<<endl;
cout<<"Your ID :"<<id<<endl;
cout<<"Your totalmarks : :"<<totalmarks<<endl;
float percentages=((float)totalmarks/500)*100;
cout<<"Your percentages :"<<percentages<<"%"<<endl;
}
};
int main()
{
int n;
cout<<"Enter Number Of students"<<endl;
cin>>n;
student s1[n];
for(int i=0;i<n;i++)
{
cout<<"Enter Information OF student :"<<i+1<<endl;
s1[i].getdata();
s1[i].showdata();
}
return 0;
}
2
12)//friend fun operator overload
#include <iostream>
class MyClass {
private:
int value;
public:
MyClass(int num) : value(num) {}
// Friend function declaration
friend MyClass operator-(const MyClass& obj1, const MyClass& obj2);
void display() const {
std::cout << "Value: " << value << std::endl;
}
};
// Friend function definition
MyClass operator-(const MyClass& obj1, const MyClass& obj2) {
int result = obj1.value - obj2.value;
return MyClass(result);
}
int main() {
MyClass obj1(10);
MyClass obj2(5);
MyClass result = obj1 - obj2;
std::cout << "obj1: ";
obj1.display();
std::cout << "obj2: ";
obj2.display();
std::cout << "result: ";
result.display();
return 0;
}
13)In computer programming, "generics" refer to a language feature that allows the creation of functions, classes, or data structures that can operate on different data types while maintaining type safety. Generics provide a way to write reusable code that works with multiple data types without sacrificing type checking at compile-time.
The main advantages of using generics include:
Code Reusability: Generics enable you to write algorithms and data structures once and use them with different types. This promotes code reuse, reducing duplication and improving maintainability.
Type Safety: Generics allow the compiler to enforce type constraints at compile-time, ensuring that the code operates on the correct data types. This catches type-related errors early in the development process.
Performance: Generics can improve performance compared to using dynamic typing or runtime polymorphism (such as virtual functions) since they avoid the overhead of dynamic type checks.
11)cammand line argument
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
ofstream f1, f2;
f1.open(argv[1]);
f2.open(argv[2]);
for(int i=0;i<10;i++){
if(i%2==0){
f1<<i<<endl;
}
else{
f2<<i<<endl;
}
}
f1.close();
f2.close();
ifstream fin;
string ch;
for(int i=1;i<argc;i++){
fin.open(argv[i]);
cout<<argv[i]<<endl;
while(!fin.eof()){
getline(fin,ch);
cout<<ch<<" ";
}
cout<<"\n\n";
fin.close();
}
return 0;
}9a)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string filename;
cout << "Enter the filename: ";
cin >> filename;
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return 1;
}
string word;
int wordCount = 0;
while (file >> word) {
wordCount++;
}
file.close();
cout << "Number of words in the file: " << wordCount << endl;
return 0;
}9b)
#include <iostream>
#include <fstream>
using namespace std;
struct Student {
string name;
int roll_number;
char grade;
};
int main() {
// Get the number of students from the user.
int n;
cout << "Enter the number of students: ";
cin >> n;
// Create a file to store student information.
ofstream outfile("student_info.txt");
// Get student information from the user.
for (int i = 0; i < n; i++) {
string name;
int roll_number;
string grade;
cout << "Enter student name " << i + 1 << ": ";
cin.ignore();
getline(cin, name);
cout << "Enter student roll number " << i + 1 << ": ";
cin >> roll_number;
cout << "Enter student grade " << i + 1 << ": ";
cin >> grade;
// Write the student information to the file.
outfile << name << endl;
outfile << roll_number << endl;
outfile << grade << endl;
}
// Close the file.
outfile.close();
// Open the file to read the student information.
ifstream infile("student_info.txt");
// Read the student information from the file.
for (int i = 0; i < n; i++) {
string read_name;
int read_roll_number;
string read_grade;
infile >> read_name;
infile >> read_roll_number;
infile >> read_grade;
// Print the student information.
cout << "Name: " << read_name << endl;
cout << "Roll number: " << read_roll_number << endl;
cout << "Grade: " << read_grade << endl;
}
// Close the file.
infile.close();
return 0;
}
#include<iostream>
using namespace std;
class complex
{
int real=0,imaginary=0;
public:
void setdata(int a,int b)
{
real=a;
imaginary=b;
}
void show()
{
cout<<real<<"+ i"<<imaginary<<endl;
}
complex add(complex x1,complex x2)
{
complex ans;
ans.real=x1.real+x2.real;
ans.imaginary=x1.imaginary+x2.imaginary;
return ans;
}
//saurabh sukla sir
complex addd(complex x1)
{
complex ans;
ans.real=real+x1.real;
ans.imaginary=imaginary+x1.imaginary;
return ans;
}
};
int main()
{
complex x1,x2,x3;
x1.setdata(5,6);
x1.show();
x2.setdata(3,9);
x2.show();
x3=x3.add(x1,x2);
x3.show();
x3=x1.addd(x2);
x3.show();
}
9
9999(wr
0 Comments