0% found this document useful (0 votes)
15 views6 pages

Passing by REFERRENCE or VALUE

Uploaded by

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

Passing by REFERRENCE or VALUE

Uploaded by

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

Passing by REFERRENCE Pass by VALUE

#include <iostream> #include <iostream>


void swap(std::string &x, void swap(std::string x,
std::string &y); std::string y);
int main(){ int main(){
std::string x = "Kool- std::string x = "Kool-
Aid"; Aid";
std::string y = "Water"; std::string y = "Water";
swap(x,y); swap(x,y);
std::cout << "X: " << x std::cout << "X: " << x
<< '\n'; << '\n';
std::cout << "Y: " << y std::cout << "Y: " << y
<< '\n'; << '\n';
return 0; return 0;
} }
void swap(std::string &x, void swap(std::string x,
std::string &y){ std::string y){
std::string temp; std::string temp;
temp = x; temp = x;
x = y; x = y;
y = temp; y = temp;
} }

// X: Water // X: Water
// Y: Kool-Aid // Y: Kool-Aid

Referring to the same address Switch values of the X and Y copies

for (size_t i {0}; i < 10; ++i) {


std::cout << "scores [" << i << "] : " << scores[i] <<
std::endl;
}

---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------

Write a txt file using <fstream> library

#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream newFile;
newFile.open("CaitlynKiramman.txt", ios::out); // write text
if(newFile.is_open()){
newFile << "Caitlyn Kiramman, I love you so much, you are the most
gorgeous girl I have ever laid my eyes on.\nPlease forgive Vi and come back to
her! She needs you!";
newFile.close();
}
newFile.open("CaitlynKiramman.txt", ios::app); // append text
if(newFile.is_open()){
newFile << "This is an append text\n";
newFile << "Vi is so adorable <3 pls Cait why did you do that!\n";
newFile.close();
}
system("pause>0");
}
Read a txt file using <fstream> library

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream newFile;
newFile.open("CaitlynKiramman.txt", ios::in); // read file in console
if(newFile.is_open()){
string line;
while(getline(newFile, line)){
cout << line << endl;
}
newFile.close();
}
system("pause>0");
}
ofstream → writing to files (output)
ifstream → reading from files (input)

#include <iostream>
#include <fstream>
int main(){
char arr[100];
std::cout<<"Enter something about Caitlyn and Violet: ";
std::cin.getline(arr,696);

std::ofstream CaitVi("CaitVi.txt");
CaitVi<<arr;
CaitVi.close();
std::cout<<"\nFile write successfully"<<std::endl<<std::endl;

// Reading from file operation


std::cout<<"This is what you put into the txt file:\n";
char arr1[696];
std::ifstream obj("CaitVi.txt");
obj.getline(arr1,696);
std::cout<<arr1;
obj.close();
}

The other two standard streams cerr and clog are used to display messages when errors occur. Error
messages are displayed on screen even if standard output has been redirected to a file.
FUNCTION POINTERS

#include <iostream>
int Add(int a,int b){
return(a+b);
}
int main(){
int (*p)(int,int); // Declare a function pointer
p = &Add; // Assign pointer p to function Add (no ‘&’ is the same so its
okay)-> p = Add also works
int c;
c = (*p)(2,3); // Dereferencing and executing the function (p)(2,3) also
works
std::cout<<c;
// Ouput is 5
return 0;
}

#include <iostream>
void printHello(){
std::cout<<"hello bitches!";
}
int main(){
void (*pPrintHello)();
pPrintHello = &printHello;
pPrintHello();
}

You might also like