C + + Fifth experiment report

Source: Internet
Author: User

#include <iostream>#include<vector>#include<string>using namespacestd;//function DeclarationvoidOUTPUT1 (vector<string> &);voidOutput2 (vector<string> &);intmain () {vector<string>likes, dislikes;//create vector<string> objects likes and dislikesLikes = {"favorite book, music, film, Paintings,anime,sport,sportsman,etc" }; cout<<"-----I like these-----"<<Endl;    OUTPUT1 (likes); Dislikes= {"running, studying, etc" }; cout<<"-----I Dislike these-----"<<Endl;    OUTPUT1 (dislikes);    Swap (likes, dislikes); cout<<"-----I likes these-----"<<Endl;    Output2 (likes); cout<<"-----I Dislikes these-----"<<Endl;    Output2 (dislikes); return 0;}//function Implementation//output The element value of the vector<string> array Object v with the following labelvoidOUTPUT1 (vector<string> &v) { for(inti =0; I!=v.size (); i++) cout<< V[i] <<Endl;}//function Implementation//output The element value of the vector<string> array Object v in an iteratorvoidOutput2 (vector<string> &v) {vector<string>::iterator ITR =V.begin ();  for(ITR; ITR! = V.end (); itr++) cout<< *itr <<Endl;}

Two. 1.

#include <iostream>usingnamespace  std; int Main () {    int *p;   The pointer is initialized    9;   Direct use of    cout << *p << Endl after initialization ;     return 0 ;}

should be changed to

#include <iostream>usingnamespace  std; int Main () {    int *p=newint(9);   pointer initialization    cout << *p << Endl;
delete p; return 0 ;}

2.

#include <iostream>using namespacestd;intfn1 () {int*p =New int(5);//request memory for pointer p, the function does not release the memory of the pointer p    return*p;//}intMain () {intA =fn1 (); cout<<"The value of a is:"<<A; return 0;}
#include <iostream>using namespacestd;intfn1 () {int*p =New int(5);//request memory for pointer pDelete p;//if it is released here, it will not be able to return the value pointed by the pointer P, the program run error    return*p;}intMain () {intA =fn1 (); cout<<"The value of a is:"<<A; return 0;}

It should therefore be replaced

#include <iostream>
using namespace Std;
int fn1 () {
int t = 5;
int *p = &t;
return *p;
}
int main () {
int A;
A=fn1 ();
cout << "The value of A is:" << A;
return 0;
}

Three.

Not completed, still in debugging

Four. Second midterm

#include <iostream>#include<string>using namespacestd;classUser { Public: User (stringNmstringPd="111111") {Name=nm; Password=PD; ID= CurrentID +1; CurrentID++;    }; User (User&u) {name=U.name; Password=U.password; ID=u.id; CurrentID++;    }; ~User () {}; voidPrintuser () {cout<<"Name:"<< name <<Endl; cout<<"Passward:"<<password<<Endl; cout<<"ID:"<<id <<Endl;    }; voidResetpassward () {intn =3; stringOldpassward;  while(n) {cout<<"Please enter the original password"<<Endl; CIN>>Oldpassward; if(Password.compare (oldpassward) = =0) {cout<<"Please enter a new password"<<Endl; CIN>>password;  Break; }            Elsecout<<"input Error,"; N--;    }    }; voidPrintcurrentid () {cout<< CurrentID <<Endl; cout<<"ID:"<<id <<Endl; cout<<"Name:"<< name <<Endl; cout<<"Passward:"<<password<<Endl; };Private:    stringname; stringpassword; intID; Static intCurrentID;};intUser::currentid =999;intMain () {User U1 ("Liurui","111111");    U1.printuser ();    U1.printcurrentid ();    U1.resetpassward (); return 0;}

The third question of the midterm

 #include <string  >using  namespace   STD;  class   book { public  : Book ( string  isbnx, string  titlex,float   Pricex);  void   print ();  private  :  string      title;     string   ISBN;  float   price;};  
#include"book.h"#include<iostream>using namespacestd; Book::book (stringISBNX,stringTitlex,floatPricex): ISBN (Isbnx), title (Titlex), Price (Pricex) {};voidBook ::p rint () {cout<<"ISBN:"&LT;&LT;ISBN <<Endl; cout<<"Title:"<< title<<Endl; cout<<"Price :"<< Price <<Endl;};
#include <iostream>#include"book.h"#include<vector>using namespacestd;intmain () {vector<Book>Books; stringISBN, title; intPrice ;  while(true) {cout<<"Enter title"<<Endl; CIN>>title; cout<<"Enter Number"<<Endl; CIN>>ISBN; cout<<"Enter Price"<<Endl; CIN>>Price ;        Book Book1 (ISBN, Title, price);        Books.push_back (BOOK1); cout<<"Enter C to continue, enter S stop"<<Endl; CharA; CIN>>A; if(A = ='s')             Break; Else;    };  for(inti =0; I < books.size (); i++) Books[i].print (); return 0;}

C + + Fifth experiment report

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.