0% found this document useful (0 votes)
61 views58 pages

C++ File

The document contains C++ code snippets demonstrating various concepts related to structures, classes, and objects. It includes examples of using structures to store and pass student data, using classes with single and multiple objects, defining member functions inside and outside classes, using inline functions, friend classes, virtual destructors, dynamic constructors, and defining constructors and destructors. The code snippets show how to apply these OOP concepts to solve problems related to storing, manipulating, and displaying student, book, and other data.

Uploaded by

satwant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views58 pages

C++ File

The document contains C++ code snippets demonstrating various concepts related to structures, classes, and objects. It includes examples of using structures to store and pass student data, using classes with single and multiple objects, defining member functions inside and outside classes, using inline functions, friend classes, virtual destructors, dynamic constructors, and defining constructors and destructors. The code snippets show how to apply these OOP concepts to solve problems related to storing, manipulating, and displaying student, book, and other data.

Uploaded by

satwant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 58

1.

/*WAP to interchange the value of two numbers without using


third variable*/
#include<iostream.h>

#include<conio.h>

void main()

int a=10,b=20;

cout<<"\n Before interchange the value is:a="<<a<<"\n b="<<b;

a=a+b;

b=a-b;

a=a-b;

cout<<"\n After interchange the value is:a="<<a<<"\n b="<<b;

getch();

OUTPUT:
2. /*wap TO PRINT THE DATA

12

123
#include<iostream.h>

#include<conio.h>

void main()

int a;

clrscr();

cout<<"Enter any numbers";

cin>>a;

for(int i=1;i<=a;i++)

for(int j=1;j<=i;j++)

cout<<j;

cout<<endl;

getch();

Output:
3. /*WAP to print the data
1

21

321

4321
#include<iostream.h>

#include<conio.h>

void main()

int a;

clrscr();

cout<<"Enter any numbers";

cin>>a;

for(int i=1;i<=a;i++)

for(int j=i;j>=1;j--)

cout<<j;

cout<<endl;

getch();

OUTPUT:
4. /*WAP to use WS manipulator*/
#include<iostream.h>

#include<conio.h>

void main()

char name[20];

cout<<"\n Enter a name:"<<endl;

cin>>ws;

cin>>name;

cout<<"\n Name is:"<<name;

getch();

OUTPUT:
5./*WAP to find the biggest number in an array*/
#include<iostream.h>

#include<conio.h>

void main()

int a[10],i,n,big;

clrscr();

cout<<"Enter the number of element";

cin>>n;

cout<<"Enter the element";

for(i=0;i<n;i++)

cin>>a[i];

big=a[0];

for(i=1;i<n;i++)

if(a[i]>big)

big=a[i];

cout<<"\n Biggest number is:"<<big;

getch();

}
OUTPUT:
6./*WAP to explain the concept of return statement without
condition*/
#include<iostream.h>

#include<conio.h>

int mul(int,int);

void main()

int x,y,z;

cout<<"\n Enter the value of x and y:"<<endl;

cin>>x>>y;

z=mul(x,y);

cout<<"Multiplied result is:"<<z;

getch();

int mul(int p,int q)

int s;

s=p*q;

return(s);

OUTPUT:
7. /*WAP with no argument and no return */
#include<iostream.h>

#include<conio.h>

void si(void);

void printline();

void main()

printline();

si();

printline();

getch();

void printline()

int i;

for(i=1;i<=40;i++)

cout<<"-";

return;

void si()

int p,r,t,s;

cout<<"\n Enter the values of Principle,rate and time:";

cin>>p>>r>>t;
s=(p*r*t)/100;

cout<<"\n Simple interest is="<<s<<endl;

return;

OUTPUT:
8. /*WAP with argument and with return value*/
#include<iostream.h>

#include<conio.h>

int simple(int,int,int);

void main()

int p,r,t,si;

clrscr();

cout<<"\n enter the value of principle,rate and time:";

cin>>p>>r>>t;

si=simple(p,r,t);

cout<<"\n Simple interest is="<<si;

getch();

int simple(int x,int y,int z)

int m;

clrscr();

m=(x*y*z)/100;

return(m);

OUTPUT:
9. /*WAP to using the concept of the value of structure variable of
library data*/
#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

struct library

char title[30];

char author[20];

int pages;

float price;

};

struct library book1;

strcpy(book1.title,"PROGRAMMING USING C++");

strcpy(book1.author,"AMARDEEP GUPTA");

book1.pages=549;

book1.price=450.50;

cout<<"\n Name of the book is"<<book1.title;

cout<<"\n Name of the author is"<<book1.author;

cout<<"\n Page of the book are"<<book1.pages;

cout<<"\n Price of the book is"<<book1.price;

getche();

}
OUTPUT:
10. /*WAP to find the average marks of two student having 3 subject
by using array of structure*/
#include<iostream.h>

#include<conio.h>

void main()

struct student

int subject1;

int subject2;

int subject3;

};

int i,n,total;

float av;

struct student std[200];

cout<<"\n Enter the number of student:";

cin>>n;

for(i=1;i<=n;i++)

cout<<"\n Enter the marks of the three subject";

cin>>std[i].subject1>>std[i].subject2>>std[i].subject3;

total=std[i].subject1+std[i].subject2+std[i].subject3;

av=total/3;

cout<<"\n AVERAGE MARKS ARE="<<av;

getch();

}
OUTPUT:
11. /*WAP to find the average the marks of 2 student having M
subject by using array within structure*/
#include<iostream.h>

#include<conio.h>

void main()

struct student

int rn;

int sub[10];

};

struct student std[100];

int i,j,total,n,m;

float av;

cout<<"\n Enter the number of student:";

cin>>n;

cout<<"\n Enter the number of subject:";

cin>>m;

for(i=1;i<=n;i++)

total=0;

for(j=1;j<=m;j++)

cin>>std[i].sub[j];

total=total+std[i].sub[j];

}
av=total/m;

cout<<"Average="<<av<<endl;

getch();

OUTPUT:
12. /*WAP to read and display student data to using passing
structure to function*/
#include<iostream.h>

#include<conio.h>

struct student

int rn;

char name[20];

char grade;

}s;

void main()

void display(student s);

clrscr();

cout<<"\n Enter the rollno,name and grade of the student:";

cin>>s.rn>>s.name>>s.grade;

display(s);

getch();

void display(student m)

cout<<"\n Rollno is:"<<m.rn;

cout<<"\n Name is:"<<m.name;

cout<<"\n Grade is:"<<m.grade;

return;

}
OUTPUT:
13. /*WAP to change the price of book by using structure with
function*/
#include<iostream.h>

#include<conio.h>

struct book

char title[20];

char author[20];

int price;

};

void main()

clrscr();

book a;

cout<<"Enter the price of book==>";

cin>>a.price;

struct book change(struct book);

a=change(a);

cout<<"New price is==>"<<a.price;

getch();

struct book change(struct book b)

b.price+=100;

return b;

}
OUTPUT:
14. /*WAP to find the Factorial of a number using structure*/
#include<iostream.h>

#include<conio.h>

struct fact

int n;

}s;

void main()

clrscr();

cout<<"Enter the number";

cin>>s.n;

int m=1;

for(int i=1;i<=s.n;i++)

m=m*i;

cout<<"Factorial of the given no is:"<<m;

getch();

OUTPUT:
15. /*WAP to print the record of a student by using structure*/
#include<iostream.h>

#include<conio.h>

void main()

struct student

int rollno;

char name[20];

char classes[20];

float fees;

};

struct student st1;

cout<<"\n Enter the data of student first rollno,name,classes,fees";

cin>>st1.rollno>>st1.name>>st1.classes>>st1.fees;

cout<<"\n Rollno of the student is:"<<st1.rollno;

cout<<"\n Name of the student is:"<<st1.name;

cout<<"\n Classes of the student is:"<<st1.classes;

cout<<"\n fees submitted by the student is:"<<st1.fees;

getch();

}
OUTPUT:
16. /*WAP to use the concept of one class with one object by taking
student data*/
#include<iostream.h>

#include<conio.h>

class student

private:

int rn;

char name[20];

public:

void read()

cout<<"\n Enter the roll number of student is:"<<endl;

cin>>rn;

cout<<"\n Enter the name of the student is:"<<endl;

cin>>name;

void write()

cout<<"\n Roll number is:"<<rn;

cout<<"\n Name is:"<<name;

};

void main()

class student std1;


clrscr();

std1.read();

std1.write();

getch();

OUTPUT:
17. /*WAP to use the concept of one class with two object*/
#include<iostream.h>

#include<conio.h>

class student

private:

int rn;

char name[20];

public:

void read()

cout<<"\n Enter the roll number of student is:"<<endl;

cin>>rn;

cout<<"\n Enter the name of the student is:"<<endl;

cin>>name;

void write()

cout<<"\n Roll number is:"<<rn;

cout<<"\n Name is:"<<name;

};

void main()

class student std1,std2;

clrscr();
std1.read();

std1.write();

std2.read();

std2.write();

getch();

OUTPUT:
18. /*WAP to show the relationship of class and object by taking
student data*/
#include<iostream.h>

#include<conio.h>

void main(void)

class student

public:

int rn;float fees;

};

class student st;

st.rn=227;

st.fees=45000;

cout<<"\n ROLL NO OF STUDENT IS:"<<st.rn;

cout<<"\n FEES PAID BY STUDENT IS:"<<st.fees;

getch();

OUTPUT:
19. /*WAP to define the member function outside the class*/

#include<iostream.h>

#include<conio.h>

class account

private:

int accno;

char name[20];

float bal;

public:

void read();

void write();

};

void account::read()

cout<<"\n Enter the account number,name and balance";

cin>>accno>>name>>bal;

void account::write()

cout<<"\n Account number is:"<<accno;

cout<<"\n Name of the customer is:"<<name;

cout<<"\n Account balance is:"<<bal;

void main()
{

class account a1;

clrscr();

a1.read();

a1.write();

getch();

OUTPUT:
20. /*WAP to find a number is even or odd by using inline function*/
#include<iostream.h>

#include<conio.h>

inline int even(int n)

return !(n%2);

void main()

int a;

clrscr();

cout<<"Enter any number";

cin>>a;

if(even(a))

cout<<a<<"is even";

else

cout<<a<<"is odd";

getch();

OUTPUT:
21. /*WAP to use Friend Class*/
#include<iostream.h>

#include<conio.h>

class xxx

private:

int x,y;

public:

xxx(int xx,int yy)

x=xx;y=yy;

friend class yyy;

};

class yyy

public:

void f1(xxx objx)

cout<<"x="<<objx.x;

void f2(xxx objy)

cout<<"\n y="<<objy.y;

}
};

void main()

xxx ob1(10,15);

yyy ob2;

ob2.f1(ob1);

ob2.f2(ob1);

getch();

OUTPUT:
22. /*WAP to define Virtual Destructor*/
#include<iostream.h>

#include<conio.h>

class Base

public:

Base(){ cout<<"Constructing Base";}

// this is a destructor:

~Base(){ cout<<"Destroying Base";}

};

class Derive: public Base

public:

Derive(){ cout<<"Constructing Derive";}

~Derive(){ cout<<"Destroying Derive";}

};

void main()

Base *basePtr = new Derive();

delete basePtr;

}
Output:

Constructing Base

Constructing Derive
23. /*WAP to display and read rollno and fees to use the Dynamic
Constructor*/
#include<iostream.h>

#include<conio.h>

class abc

int rn;

float fees;

public:

abc(int a,float b)

rn=a;

fees=b;

abc(abc&m)

rn=m.rn;

fees=m.fees;

cout<<"\n Copy constructor at work";

};

void main()

clrscr();

int x;

float y;
cout<<"\n Enter the rollno of the student";

cin>>x;

cout<<"\n Enter the fees of student";

cin>>y;

abc m1(x,y);

cout<<"\n Student rollno is"<<x;

cout<<"\n Student fees is"<<y;

getch();

OUTPUT:
24. /*WAP to define Constructor and Destructor*/
#include<iostream.h>

#include<conio.h>

#include<string.h>

class student

private:

int rn;

char name[20];

public:

student() //constructor define

rn=27;

strcpy(name,"SUKHBINDER");

void dispdata()

cout<<"\n ROLL NO is"<<rn;

cout<<"\n NAME is"<<name;

~student() //destructor define

cout<<"Destructor invoked";

};

void main()
{

clrscr();

class student s1;

s1.dispdata();

getch();

OUTPUT:
25. /*WAP to set Overloading of Constructor*/
#include<iostream.h>

#include<conio.h>

class student

private:

int rn;

public:

student()

rn=0;

student(int n)

rn=n;

} //overloading of constructor

~student() //destructor define

void getdata()

cout<<"\n Enter the Roll number:";

cin>>rn;

void dispdata()

{
cout<<"\n ROLL NUMBER is:"<<rn;

};

void main()

clrscr();

class student a;

class student b(227);

a.getdata();

a.dispdata();

b.dispdata();

getch();

OUTPUT:
26. /*WAP to Assignment operator overloading*/
#include<iostream.h>

#include<conio.h>

class abc

private:

int a;

float b;

public:

abc(int,float);

void display();

};

abc::abc(int x,float y)

a=x;

b=y;

void abc::display()

cout<<"Integer value is:"<<a<<"\n";

cout<<"Float value is:"<<b<<"\n";

void main ()

clrscr();

abc m1(10,22.2);
abc m2(30,7.7);

cout<<"First object has value"<<"\n";

m1.display();

cout<<"Second object has value"<<"\n";

m2.display();

getch();

OUTPUT:
27. /*WAP of Single Inheritance*/
#include<iostream.h>

#include<conio.h>

class abc

protected:

char name[20];

char village[10];

int age;

};

class abcd:public abc

float hight;

float weight;

public:

void getdata()

cout<<"\n Enter the name,village and age:";

cin>>name>>village>>age;

cout<<"\n Enter Hight and Weight:";

cin>>hight>>weight;

void show()

cout<<"\n Name:"<<name<<"\n Village:"<<village<<"\n Age:"<<age<<"years";

cout<<"\n Hight:"<<hight<<"Feets"<<"\n Weight:"<<weight<<"Kg";


}

};

void main()

clrscr();

abcd x;

x.getdata();

x.show();

getch();

OUTPUT:
28. /*WAP to initialize the pointer values*/
#include<iostream.h>

#include<conio.h>

void main()

int x,*q;

x=200;

q=&x;

cout<<x<<"is stored at address"<<&x<<endl;

cout<<x<<"is stored at address"<<q<<endl;

cout<<*q<<"is stored at address"<<q<<endl;

cout<<*&x<<"is stored at address"<<&x;

getch();

OUTPUT:
29. /*WAP to access the value by using pointer*/
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

char ab;

int x;

float p,q;

ab='Z';

x=28;

p=3.12;

q=4.7;

cout<<ab<<"is stored at address "<<&ab<<endl;

cout<<x<<"is stored at address "<<&x<<endl;

cout<<p<<"is stored at address "<<&p<<endl;

cout<<q<<"is stored at address "<<&q<<endl;

getch();

OUTPUT:
30. /*WAP to using the concept of Pointer to Pointer*/
#include<iostream.h>

#include<conio.h>

void main(void)

int a,*p1,**p2;

clrscr();

a=27;

p1=&a;

p2=&p1;

cout<<"The value of A is stored at address "<<p1;

cout<<"\n Address of P1 is stored at address "<<p2;

cout<<"\n The value of A using pointer to pointer is "<<**p2;

getch();

OUTPUT:
31. /*WAP to print a table using Pointer*/
#include<iostream.h>

#include<conio.h>

void main()

int n,i,p,*pt;

clrscr();

cout<<"Enter a number whose table is to be generated:"<<endl;

cin>>n;

cout<<endl<<"The required table is given below:"<<endl;

*pt=1;

do

p=*pt*n;

cout<<n<<"*"<<*pt<<"="<<p<<endl;

(*pt)++;

while(*pt<=10);

getch();

OUTPUT:
32. /*WAP to use of Overloading function*/
#include<iostream.h>

#include<conio.h>

void display(int a)

cout<<"\n Function taking single element is:\n";

cout<<a;

void display(int a,int n)

cout<<"\n Function taking double element is:\n";

cout<<a<<endl;

cout<<n;

void main()

clrscr();

void display(int);

void display(int,int);

int a=10;

int b=20;

display(a);

display(a,b);

getch();

OUTPUT:
33. /*WAP to use Virtual function*/
#include<iostream.h>

#include<conio.h>

class base

public:

virtual void display()

cout<<"\n Base";

};

class derived:public base

public:

void display()

cout<<"\n Derived";

};

void main()

class base*ptr=new derived();

ptr->display();

getch();

OUTPUT:
34. /*WAP to illustrate the concept of Static Binding(Early Binding)*/
#include<iostream.h>

#include<conio.h>

class base

public:

void display()

cout<<"\n Base";

};

class derived:public base

public:

void display()

cout<<"\n Derived";

};

void main()

class base*ptr=new derived();

ptr->display();

getch();

OUTPUT:
35. /*WAP to using the Write() function from the file*/

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

int main()

ofstream myfile;

myfile.open("example.txt");

myfile<<"Writting this to a file /n";

myfile.close();

return 0;

}
36. /*WAP to using the Read() function from the file*/

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

int main()

ofstream abc;

abc.open("Raj.dat");

for(int i=1;i<=100;i++)

abc<<i<<endl;

abc.close();

return 0;

}
37./*WAP to read a number from a text file*/

#include<iostream.h>

#include<fstream.h>

int n;

void main()

ifstream num;

num.open("Sukhbinder");

num>>n;

cout<<"output is"<<n;

num.close();

You might also like