v c++ program file

67
VC++ PROGRAM FILE USING DEVELOPER STUDIO SUBMITTED TO : MS. SHWETA SUBMITTED By : NAVJOT KAUR Roll No : 9480 MSC-I.T(3 rd Sem)

Upload: sonu-pal

Post on 10-Nov-2014

22 views

Category:

Documents


2 download

Tags:

DESCRIPTION

V c++ Program File

TRANSCRIPT

Page 1: V c++ Program File

VC++ PROGRAM FILEUSING DEVELOPER STUDIO

SUBMITTED TO : MS. SHWETA

SUBMITTED By : NAVJOT KAURRoll No : 9480MSC-I.T(3rd Sem)

Page 2: V c++ Program File

INDEX

S.NO TOPICS PAGE NO REMARKS

1. PROGRAM DEMONSTRATING ADD 2 NO. USING CLASSES

3-4

2. PROGRAM DEMONSTRATING FABONICII SERIES 5-6

3. PROGRAM DEMONSTRATING DEFAULT CONSTRUCTOR

7-8

4. PROGRAM DEMONSTRATING CONSTRUCTOR OVERLOADING

9-10

5. PROGRAM DEMONSTRATING PASSING 2 VALUES INT AND FLOAT VALUES

11-12

6. PROGRAM DEMONSTRATING MARKSHEETOF EXAMINATION

13-15

7. PROGRAM DEMONSTRATING NESTED CLASSES 16-17

8. PROGRAM DEMONSTRATING COPY CONSTRUCTOR 18-19

9. PROGRAM DEMONSTRATING INLINE FUNCTIONS 20-21

10. PROGRAM DEMONSTRATING VIRTUAL FUNCTIONS 22-23

11. PROGRAM DEMONSTRATING BUBBLE SORT 24-25

12. PROGRAM DEMONSTRATING BINARY OVERLOADING 26-27

13. PROGRAM DEMONSTRATING REVERSE OF DIGIT 28-29

14. PROGRAM DEMONSTRATING FACTORIAL OF NO. 30-31

15. PROGRAM DEMONSTRATING ARRAY OF OBJECTS 32-34

16. PROGRAM DEMONSTRATING MATRIX MULTIPLICATION

35-37

17. DOCUMENT VIEW ARCHITECTURE 38-39

18. SDI 40-4319. MDI 44-47

2

Page 3: V c++ Program File

PROGRAM TO ADD 2 NO. USING CLASSES

#include<iostream>

using namespace std;class sample{private:

int a,b,c;public:

void get(){

cout<<"enter 2 no.";cin>>a>>b;

}void display(){

c=a+b;cout<<"result is"<<c<<endl;

}

};void main(){

sample s;s.get();s.display();

}

3

Page 4: V c++ Program File

4

Page 5: V c++ Program File

PROGRAM TO DISPLAY FABNOCII SERIES

#include<iostream>#include<conio>

using namespace std;class sample{private:

int i,a,b,c;public:

void get(){

a=0;b=1;cout<<a<<b;

}void display(){

for(i=0;i<=5;i++){

c=a+b;cout<<c;a=b;b=c;}

}

};void main(){

sample s;s.get();s.display();getch();

}

5

Page 6: V c++ Program File

6

Page 7: V c++ Program File

PROGRAM OF DEFAULT CONSTRUCTOR AND DESTRUCTOR

#include<iostream>

using namespace std;

class Constr{private:

char name[20];

public:Constr(){

cout<<" Hello this is default constructor automatically invokes as soon as object of class is created"<<endl;

cout<<"enter name"<<endl;cin>>name;cout<<"entered name is"<<name<<endl;

}~Constr(){

cout<<"now default destructorr initialzed bt explixitly"<<endl;}

};

void main(){

Constr c;

}

7

Page 8: V c++ Program File

8

Page 9: V c++ Program File

PROGRAM OF CONSTRUCTOR OVERLOADING

#include<iostream>

using namespace std; class over { private:

int a,b; public:

over() {

cout<<"hi..!!!..this is default constructor automatically invoked"<<endl; } over(int e) {

int i= e; cout<<" the no. passed for the 1 argument constructor is"<<i<<endl;

} over(int k, int l) {

a= k; b= l; int t= a+b; cout<< "the result of constructor with 2 argument is" <<endl<<t<<endl<<"this is constructor

overloading"<<endl; }

}; void main() {

over o; over o1(5); over o2(4,5);

}

9

Page 10: V c++ Program File

10

Page 11: V c++ Program File

PROGRAM OF PASSING 2 VALUES : INT AND FLOAT IN CONSTRUCTOR

#include<iostream>

using namespace std;

class pass{private:

int a;float b;

public:pass(int i, float j){

a= i;b= j;float c= a+b;cout<<" the result of addition of int and float value is "<<c<<endl;

}};void main(){

pass p(5,5.7);}

11

Page 12: V c++ Program File

12

Page 13: V c++ Program File

PROGRAM TO DEMONSTRATE MARKSHEET OF EXAMINATION….#include<iostream>using namespace std;

class mark{private:public:

void dis1(){

cout<<"roll no. is 1"<<endl<<" name is pooja"<<endl<<"marks in maths is 70"<<endl<<"marks in c++ is 80"<<endl;

}void dis2()

{cout<<"roll no. is 2"<<endl<<" name is aviza"<<endl<<"marks in maths is 80"<<endl<<"marks in

c++ is 60"<<endl;}

};class sheet : public mark{private:

int rollno;public:

void get_data(){

cout<<" enter roll no. of the student";cin>>rollno;

if(rollno== 1){

dis1();}else if(rollno== 2){

dis2();}else{

cout<<"wrong roll no is entered";}

}};void main(){

sheet s;s.get_data();

13

Page 14: V c++ Program File

14

Page 15: V c++ Program File

15

Page 16: V c++ Program File

PROGRAM OF NESTED CLASSES

#include<iostream>using namespace std;class Host{public: class Nested { public:

int a,b,c,d; void PrintMe() { cout<<"enter 2 no.";

cin>>a>>b; c=a+b; d=a*b; cout<<"sum of 2 no.s is"<<c<<endl; cout<<"product of 2 no."<<d<<endl;}

};};

void main(){ Host::Nested foo; foo.PrintMe();

}

16

Page 17: V c++ Program File

17

Page 18: V c++ Program File

PROGRAM TO DEMONSTRATE COPY CONSTRUCTOR

#include<iostream>using namespace std;class code{

int a,b;public:

code(){

a=0;b=0;

}code(code& i){

a= i.a;b= i.b;

}void display(){

cout<<"a="<<a<<"b="<<b<<endl;}

};void main(){

code c1,c2(c1);c1.display();cout<<"now copy constructor will display the same"<<endl;c2.display();

}

18

Page 19: V c++ Program File

19

Page 20: V c++ Program File

PROGRAM TO DEMONSTRATING THE COPYING OF VALUE USING ANOTHER VARIABLE

#include<iostream>using namespace std;class code{

int a,b,c,d;public:

code(){

a=0;b=0;

}code(code& i){

c= i.a;d= i.b;

}void display(){

cout<<"c="<<c<<"d="<<d<<endl;}

};void main(){

code c1;

cout<<"now copy constructor will display the same"<<endl;code c2(c1);c2.display();

}

20

Page 21: V c++ Program File

21

Page 22: V c++ Program File

PROGRAM TO DEMONSTRATING INLINE FUNCTIONS#include<iostream>

using namespace std;

class inn{private:

float a,radius;public:

void disp(){

cout<<"enter radius";cin>>radius;a=area(radius);cout<<a<<endl;

}

inline float area(float r){

return(3.14*r*r);}};void main(){

inn i;i.disp();

}

22

Page 23: V c++ Program File

23

Page 24: V c++ Program File

PROGRAM TO DEMONSTRATING VIRTUAL FUNCTIONS

#include<iostream>

using namespace std;

class base{

int a;public:

base(){

a=0;}virtual void dis(){

cout<<a<<endl;}

};class der : public base{

int b;public:

der(){

b=1;}void dis(){

cout<<"derived class display calls";cout<<b<<endl;

}

};void main(){

base s;s.dis();

der *d;der r;d= &r;d->dis();}

24

Page 25: V c++ Program File

25

Page 26: V c++ Program File

PROGRAM TO DEMONSTRATING BUBBLE SORT

#include<iostream>using namespace std;

class bb{

int a[40],n,i,j,temp;

public:void display(){

cout<<"enter no. of elements";cin>>n;cout<<"enter array";cin>>a[i];

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

for(j=1;j<n-i;j++){

if(a[j]>a[j+1]){

temp=a[j];a[j]=a[j+1];a[j+1]=temp;}

}}

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

cout<<a[i];}

}};void main(){

bb b;b.display();

}

26

Page 27: V c++ Program File

PROGRAM DEMONSTRATING OPERATOR OVERLOADING

27

Page 28: V c++ Program File

#include<iostream>using namespace std;class sample{

float a,b;

public:sample(){};

sample(float x, float y){

a=x;b=y;

}sample operator+(sample);

void display(){

cout<<"a is "<<a<<'\t'<<"b is "<<b<<endl;}

};sample sample :: operator+(sample c)

{sample temp;temp.a= a+c.a;temp.b= b+c.b;return(temp);

}void main(){sample x,y,z;x= sample(1.2,1.2);y= sample(1.2,1.9);

z= x+y;x.display();y.display();cout<<" through operator overloadinggg....";z.display();}

28

Page 29: V c++ Program File

29

Page 30: V c++ Program File

PROGRAM DEMONSTRATING REVERSE OF TWO DIGIT NO.

#include<iostream>using namespace std;class reversDigits{int num;

public:

void display() {

cout<<"enter no.";cin>>num;

int rev_num = 0;

while(num > 0)

{ rev_num = rev_num*10 + num%10; num = num/10;

} cout<<rev_num<<endl;

}

};void main(){reversDigits s;s.display();}

30

Page 31: V c++ Program File

31

Page 32: V c++ Program File

PROGRAM DEMONSTRATING FACTORIAL OF NO.

#include<iostream>using namespace std;class fac{int x;

public:

void calfactorial(){

cout<<"enter no.";cin>>x;

int fx = 1; if (x==1)

cout<<"1"; else for (int i = 1;i<= x;++i) {

fx = fx * i; } cout<<fx<<endl; }

};void main(){fac s;s.calfactorial();}

32

Page 33: V c++ Program File

33

Page 34: V c++ Program File

PROGRAM DEMONSTRATING AVERAGE OF STUDENTS USING ARRAYS OF CLASS OBJECTS

#include<iostream>using namespace std;class student{int roll;char name[20];

public:

int marks;void getdata(){

cout<<"enter rollno."<<endl;cin>>roll;

cout<<"enter name"<<endl;cin>>name;cout<<"enter marks"<<endl;cin>>marks;

}

};

void main(){

student s[4];int sum=0;int mean;for(int i=0;i<4;i++){

cout<<"details"<<endl<<i+1;s[i].getdata();sum= sum+s[i].marks;

}mean=sum/4;cout<<"average"<<mean<<endl;;

}

34

Page 35: V c++ Program File

35

Page 36: V c++ Program File

PROGRAM DEMONSTRATING MATRIX MULTIPLICATION#include<iostream>#include<iomanip>using namespace std;class student{int a[10][10],b[10][10],s[10][10],m,i,j,n;public:

void getdata(){

cout<<"enter order of matrix";cin>>m>>n;cout<<"enter the elements of first matrix";for(i=1;i<=m;i++)

for(j=1;j<=n;j++)cin>>a[i][j];

cout<<"enter the elements of 2nd matrix";for(i=1;i<=m;i++)

for(j=1;j<=n;j++)cin>>b[i][j];

cout<<" matrix ";for(i=1;i<=m;i++)

for(j=1;j<=n;j++)s[i][j]= a[i][j]*b[i][j];

cout<<"multiplication"<<endl;for(i=1;i<=m;i++){

for(j=1;j<=n;j++){

cout<<setw(5)<<s[i][j];}

cout<<endl;}}

};

void main(){ student s;

s.getdata();}

36

Page 37: V c++ Program File

37

Page 38: V c++ Program File

CREATING SINGLE DOCUMENT INTERFACE APPLICATIONS

Today you will learn a different way of approaching application development with Visual C++ than you have used with the previous days’ lessons. Today you will learn how to create Single Document Interface (SDI) applications. An SDI application is a document-centric application that can only work with one document at a time, and can only work with one type of document.

Some good examples of SDI applications are Notepad, WordPad, and Paint. All of these applications can do only one type of task and can only work on one task at a time. WordPad is almost like an SDI version of Word. It’s able to perform a large number of the tasks that Word does, but although Word allows you to work on numerous documents at the same time, WordPad limits you to only one document.

Some of the things that you will learn today are• The Document/View architecture that Visual C++ uses for creating SDIapplications.• How to create an SDI application shell.• How to separate your data from the visual representation of the data.• How to encapsulate your data in its own C++ class.• How to create interaction between the data and the menus.

38

Page 39: V c++ Program File

THE DOCUMENT/VIEW ARCHITECTURE

When you create an SDI application, more classes are created for an SDI applicationthan for a dialog-style application. Each of these classes serves a specific purpose in howSDI applications operate. Ignoring the About window dialog class, four specific classesmake up an SDI application:• The CWinApp-derived class• The CFrameView-derived class• The CDocument-derived class• The CView-derived class

The CWinApp class creates all the other components in the application. It is the class thatreceives all the event messages and then passes the messages to the CFrameView andCView classes.

The CFrameView class is the window frame. It holds the menu, toolbar, scrollbars, andany other visible objects attached to the frame. This class determines how much of thedocument is visible at any time. Very little (if any) of your programming efforts on SDIapplications will require making any modifications or additions to either of these firsttwo classes.

The CDocument class houses your document. This class is where you will build the datastructures necessary to house and manipulate the data that makes up your document. This class receives input from the CView class and passes display information to the CView class. This class is also responsible for saving and retrieving the document data from files.

The CView class is the class that displays the visual representation of your document forthe user. This class passes input information to the CDocument class and receives displayinformation from the CDocument class. Most of the coding that you will do for this classconsists of drawing the document for the user and handling the input from the user. TheCView class has several descendent classes that can be used as the ancestor for the viewclass.

10

39

Page 40: V c++ Program File

TABLE 10.1. THE CView DESCENDENT CLASSES. CEditView Provides the functionality of a edit box control. Can be used to

implement simple text-editor functionality.

CFormView The base class for views containing controls. Can be used to provide form-based documents in applications..

CListView Provides list-control functionality in the Document/View architecture. CRichEditView Provides character and paragraph formatting functionality.

CScrollView Provides scrolling capabilities to a CView class. CTreeView Provides tree-control functionality in the Document/View architecture.

All four of these classes work together to make up the full functionality of an SDI application, as shown in Figure 10.1. By taking advantage of this architecture, you can buildpowerful document-centric applications with relative ease.

40

Page 41: V c++ Program File

CREATING AN SDI APPLICATION

To get a good idea of how the Document/View architecture works, and of how you canuse it to build applications, you will build a new version of the drawing application youcreated on Day 3, “Allowing User Interaction—Integrating the Mouse and Keyboard inYour Application.” In this version, the user’s drawing will be persistent, which means itis not erased each time another window is placed in front of the application. This versionwill also be able to save and restore drawings.

BUILDING THE APPLICATION SHELLTo create the application shell for today’s application, follow these steps:1. Create a new AppWizard project. Name the project Day10.2. On the first step of the AppWizard, select Single Document.3. Use the default values on the second step of the AppWizard.4. On the third step of the AppWizard, uncheck the support for ActiveX Controls.5. On the fourth step of the AppWizard, leave all the default values. Click theAdvanced button.6. In the Advanced Options dialog, enter a three-letter file extension for the files thatyour application will generate (for example, dhc or dvp). Click the Close button toclose the dialog and then click Next to move to the next step of the AppWizard.7. Use the default settings on the fifth step of the AppWizard.8. On the sixth and final AppWizard step, you can choose the base class on whichyour view class will be based. Leave the base class as CView and click Finish. TheAppWizard will generate the application shell.Creating Single Document Interface Applications 20310CREATING A LINE CLASSOne of the first issues that you will need to tackle is how to represent your data in thedocument class. For the drawing application, you have a series of lines. Each line consistsof a starting point and ending point. You might think that you can use a series ofpoints for the data representation. If you do this, you also have to make special accommodationsfor where one series of lines between points ends and the next begins. Itmakes much more sense to represent the drawing as a series of lines. This allows you tostore each individual line that is drawn on the window without having to worry whereone set of contiguous lines ends and where the next begins.Unfortunately, the Microsoft Foundation Classes (MFC) does not have a line objectclass, although it does have a point object class (CPoint). I guess you’ll just have to createyour own line class by following these steps:

41

Page 42: V c++ Program File

1. In the Class View tab of the workspace pane, select the top-level object in the tree(Day10 classes). Right-click the mouse and select New Class from the pop-upmenu.2. In the New Class dialog, select Generic Class for the class type. Enter CLine for theclass name and click in the first line in the Base Class list box. Enter CObject as thebase class, leaving the class access as public, as in Figure 10.2.3. When you click the OK button to add the CLine class, you may be told that theClass Wizard cannot find the appropriate header file for inheriting the CLine classfrom the CObject class, as in Figure 10.3. Click on the OK button on this messagebox.FIGURE 10.2.The New Class Wizard.

204 Day 10

The appropriate header class is already included in the CLine class files. Untilyour compiler complains because it can’t find the definition for the CObjectclass, don’t worry about this message. However, if you are using a base classthat’s a bit further down the MFC class hierarchy, you might need to heedthis message and add the ap

42

Page 43: V c++ Program File

DRAWING THE CLINE CLASS

To follow correct object-oriented design, your CLine class should be able to draw itself sothat when the view class needs to render the line for the user, it can just pass a messageto the line object, telling it to draw itself. To add this functionality, follow these steps:1. Add a new function to the CLine class by selecting Add Member Function from thepop-up menu.2. Specify the function type as void and the function declaration as Draw(CDC *pDC).3. Add the code in Listing 10.2 to the Draw function you just added.

LISTING 10.2. THE CLine Draw FUNCTION.1: void CLine::Draw(CDC * pDC)2: {3: // Draw the line4: pDC->MoveTo(m_ptFrom);5: pDC->LineTo(m_ptTo);6: }

43

Page 44: V c++ Program File

WHAT IS AN MDI APPLICATION?

As far as coding an MDI application with Visual C++, there’s little differencebetween creating an SDI and an MDI application. However, when you getdeeper into the two application styles, you’ll find quite a few differences. Although anSDI application allows the user to work on only one document at a time, it also normallylimits the user to working on a specific type of document. MDI applications not onlyenable the user to work on multiple documents at the same time, but also MDI applicationscan allow the user to work on multiple types of documents.An MDI application uses a window-in-a-window style, where there is a frame windowaround one or more child windows. This is a common application style with many popularsoftware packages, including Word and Excel.Architecturally, an MDI application is similar to an SDI application. In fact, with a simpleMDI application, the only difference is the addition of a second frame class to theother classes that the AppWizard creates, as shown in Figure 11.1. As you can see, theDocument/View architecture is still very much the approach you use for developing MDIapplications as well as SDI applications.When you create an MDI application, you will create just one more class than you createdwith an SDI application. The classes are• The CWinApp derived class• The CMDIFrameWnd derived class• The CMDIChildWnd derived class• The CDocument derived class• The CView derived class

44

Page 45: V c++ Program File

FIGURE 11.1.The MDI Document/View architecture.

11

The two MDI derived classes, CMDIFrameWnd (the CMainFrame class in your project) andCMDIChildWnd (the CChildFrame class in your project), are the only two classes that aredifferent from the SDI application that you created.The first of these two classes, the CMDIFrameWnd-derived CMainFrame, is the main frameof the application. It provides an enclosed space on the desktop within which all applicationinteraction takes place. This frame window is the frame to which the menu and toolbarsare attached.The second of these two classes, the CMDIChildWnd-derived CChildFrame class, is theframe that holds the CView class. It is the frame that passes messages and events to theview class for processing or display.In a sense, the functionality of the frame class in the SDI application has been split intothese two classes in an MDI application. There is additional support for running multiplechild frames with their own document/view class instances at the same time.

45

Page 46: V c++ Program File

CREATING AN MDI DRAWING PROGRAMTo get a good understanding of just how alike the Document/View architectures are forthe SDI and MDI applications, today you will implement that same drawing applicationthat you created yesterday, only this time as an MDI application.Building the Application Shell

To create the application shell for today’s application, follow these steps:1. Create a new AppWizard project. Name the project Day11.2. On the first step of the AppWizard, select Multiple Documents, as shown inFigure

Specifying an MDI

.

3. Use the default values on the second step of the AppWizard.4. On the third step of the AppWizard, uncheck the support for ActiveX Controls.5. On the fourth step of the AppWizard, leave all the default values. Click theAdvanced button.6. In the Advanced Options dialog, enter a three-letter file extension for the files thatyour application will generate (for example, dhc or dvp). Click the Close button toclose the dialog and then click Next to move to the next step of the AppWizard.7. Use the default settings on the fifth step of the AppWizard.8. On the sixth and final AppWizard step, leave the base class as CView and clickFinish. The AppWizard generates the application shell.

46

Page 47: V c++ Program File

BUILDING THE DRAWING FUNCTIONALITY

Because you are creating the same application that you created yesterday, only as anMDI application this time, you need to add the same functionality to the application thatyou added yesterday. To save time, and to reemphasize how alike these two applicationarchitectures are, perform the same steps you did yesterday to create the CLine class andadd the functionality to the CDay11Doc and CDay11View classes. Add the support into theCDay11Doc and CLine classes for selecting colors and widths, but do not add any menuevent message handlers or create the color menu. When you finish adding all that functionality,you should have an application in which you can open multiple drawings, alldrawing with only the color black.

47