classes in c++ dr. ahmed telba. file in c++ #include using namespace std; int main () { char...

55
Classes in C++ Dr. Ahmed Telba

Upload: sheila-lindsey

Post on 13-Jan-2016

264 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

Classes in C++

Dr. Ahmed Telba

Page 2: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

File in C++• #include <iostream>• #include <fstream>• #include <cmath>• #include <cstdlib>• using namespace std;• int main () • {• char filename[50];• ifstream Ahmed;• cin.getline(filename,50);• Ahmed.open(filename);• if(!Ahmed.is_open()){• exit(EXIT_FAILURE);• }• char word[50];• while(Ahmed.good()){• cout<<word<<" ";• Ahmed>>word;}• • system("pause");• return 0;}

Page 3: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• using namespace std;• class AhmedClass{• public: // set variable • void setName(string x)• { name=x;}• string getName(){• return name;• }• • private: // get variable• string name; }; • • int main(){ • AhmedClass bo; // create object bo• bo.setName(" This is Class Test "); • cout<< bo.getName();// .bo object separator

• system("pause");• return 0;• }

Page 4: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // structural • #include <iostream>• using namespace std;• struct student {• char itsName[200];• int itsNumber;• float gradesOfCourses[5];• float itsAvg;• }• ;• int main()• {• student Ahmed;• for(int i = 0;i < 5;i++) {• cout << "Enter the grade of course" << i+1 << ":\t";• cin >> Ahmed.gradesOfCourses[i];• }• int total=0;• for( i=0; i< 5;i++)• total+=Ahmed.gradesOfCourses[i];• int avg=total/ 5;•• Ahmed.itsAvg = (avg*4) /100;• cout << Ahmed.itsAvg << endl;•• return 0;• }

Page 5: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• using namespace std;•• struct student {• char itsName[200];• int itsNumber;• float gradesOfCourses[10]; • float itsAvg;• }• ;• int main()• {• student Ahmed;• for(int i = 0;i < 10;i++) { • cout << "Enter the grade of course" << i+1 << ":\t";• cin >> Ahmed.gradesOfCourses[i];• }• int total=0;• for( int i=0; i<10;i++) • total+=Ahmed.gradesOfCourses[i];•• float avg=total/ 10; •• Ahmed.itsAvg = avg; • cout <<"is the avaarage=" <<Ahmed.itsAvg << endl;• return 0;• }

Page 6: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

Classes in C++

• Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.

• An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.

• Classes are defined using either keyword class or keyword struct, with the following syntax:

class class_name { access_specifier_1: member1; access_specifier_2: member2; ...} object_names;

Page 7: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

Class in C++• class Time• {• private:• int hor ,minute ,second;• public:• void settime(int,int,int);• void printstandard ();• void printmilitary();• };

Page 8: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• class rectangle• {• int x,y;• public:• void set_value(int,int);• int area();• };

Page 9: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• #include <string>• using namespace std;• class Man• {• public:• /* Man()// constractor• {• name = "un named";• age = 0;• address = " un define";• };• string get_name(){return name;} */ take value from memory its not real if public is not define• private:• string name;• int age;• string address;

• };

• int main( )• {

• Man a;• cout<<a.get_name()<<endl;• system("pause");• return 0;• }

Page 10: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• #include <string>• using namespace std;• class Human• {• public:• Human()// constractor• {• name = "un named";• age = 0;• address = " un define";• }

• Human(string H_name,int H_age,string H_address) • • { name =H_name;• age =H_age;• address = H_address; • } • string get_name(){return name;} • int get_age(){return age;} • void raise_age(int raise)• {• age+=raise;• }• private:• string name;• int age;• string address;• };

• int main( )• { • Human a("Ahmed",50,"Riyadh");• cout<<a.get_name()<<endl;• cout<<a.get_age()<<endl;• a.raise_age(5);• cout<<a.get_age()<<endl;• system("pause");• return 0;• }

Page 11: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream.h>• #include <cstdlib> • using namespace std; • class rectangle• {• int x ,y;• public :• void set_value(int a,int b)• {• x=a;• y=b;• }

• int area()• {• return x*y;• }• };• main()• {• rectangle rect1,rect2;• rect1.set_value(3,4);• rect2.set_value(5,6);• cout<<"Rect1 Area = "<<rect1.area()<<endl;• cout<<"Rect2 Area = "<<rect2.area()<<endl;• system("PAUSE");

• }

Page 12: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream.h>• class Part• {• private:• int modelnumber;• double cost;• public:• void SetPart(int mn, double c) //set data• {• modelnumber = mn;• cost = c;• }• void ShowPart() //display data• {• cout << "Model " << modelnumber;• cout << ", costs $" << cost << endl;• }• };

• int main()• {• Part part1, part2, part3;• part1.SetPart(6245, 220);• part2.SetPart(6246, 185);• part3.SetPart(6247, 150);• part1.ShowPart();• part2.ShowPart();• part3.ShowPart();• system("pause");• return 0;• }

Page 13: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• #include <string>• using namespace std;• class AhmedClass• {• public:• void Cool()// constractor• {• cout<< " Today is very cool tray to help the poor family"<<endl;• }

• • };

• int main( )• { • AhmedClass AhmedObject;

• AhmedObject.Cool();• system("pause");• return 0;• }

Page 14: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• //Using Variables in Classes• #include <iostream>• #include <string>• using namespace std;• class AhmedClass• {• public:• void Cool()// constractor• {• cout<< " Today is very cool tray to help the poor family"<<endl;• }

• • };

• int main( )• { • AhmedClass AhmedObject;

• AhmedObject.Cool();• system("pause");• return 0;• }

Page 15: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• #include <string>• using namespace std;• class AhmedClass• {• public:• void setName(string x){• name = x;• • }• string getName(){• return name;}• private: • string name;• };

• int main( )• { • AhmedClass bo;• bo.setName("Asalamualikom");• cout<<bo.getName();• system("pause");• return 0;• }

Page 16: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• class Box• {• public:• double length; // Length of a box• double breadth; // Breadth of a box• double height; // Height of a box• };

Page 17: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // classes example#include <iostream>using namespace std;

class Rectangle { int width, height; public: void set_values (int,int); int area() {return width*height;}};

void Rectangle::set_values (int x, int y) { width = x; height = y;}

int main () { Rectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0;}

Page 18: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• using namespace std;• class Ahmed {• public:• void set_values (){• cout<< "Class Test Ahmed"<<endl;}• };• int main () • {• Ahmed Ahmedobject;• Ahmedobject.set_values ( );• system("pause");• return 0;• }

Page 19: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // classes example• #include <iostream>• using namespace std;

• class Rectangle {• int width, height;• public:• void set_values (int,int);• int area() {return width*height;}• };

• void Rectangle::set_values (int x, int y) {• width = x;• height = y;• }

• int main () {• Rectangle rect;• rect.set_values (3,4);• cout << "area: " << rect.area()<<endl;• system("pause");• return 0;• }

Page 20: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // example: one class, two objects• #include <iostream>• using namespace std;

• class Rectangle {• int width, height;• public:• void set_values (int,int);• int area () {return width*height;}• };

• void Rectangle::set_values (int x, int y) {• width = x;• height = y;• }

• int main () {• Rectangle rect, rectb;• rect.set_values (3,4);• rectb.set_values (5,6);• cout << "rect area: " << rect.area() << endl;• cout << "rectb area: " << rectb.area() << endl;• system("pause");• return 0;

Page 21: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // classes and uniform initialization• #include <iostream>• using namespace std;• class Circle {• double radius;• public:• Circle(double r) { radius = r; }• double circum() {return 2*radius*3.14159265;}• };• int main () {• Circle foo (10.0); // functional form• Circle bar = 20.0; // assignment init.• // Circle baz {30.0}; // uniform init.• // Circle qux = {40.0}; // POD-like• cout << "foo's circumference: " << foo.circum() << '\n';• system("pause");• return 0;• }

Page 22: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // member initialization• #include <iostream>• using namespace std;

• class Circle {• double radius;• public:• Circle(double r) : radius(r) { }• double area() {return radius*radius*3.14159265;}• };

• class Cylinder {• Circle base;• double height;• public:• Cylinder(double r, double h) : base (r), height(h) {}• double volume() {return base.area() * height;}• };

• int main () {• Cylinder foo (10,20);

• cout << "foo's volume: " << foo.volume() << '\n';• system("pause");• return 0;• }

Page 23: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // pointer to classes example• #include <iostream>• using namespace std;

• class Rectangle {• int width, height;• public:• Rectangle(int x, int y) : width(x), height(y) {}• int area(void) { return width * height; }• };

• int main() {• Rectangle obj (3, 4);• Rectangle * foo, * bar, * baz;• foo = &obj;• bar = new Rectangle (5, 6);• // baz = new Rectangle { {2,5}, {3,6} };• cout << "obj's area: " << obj.area() << '\n';• cout << "*foo's area: " << foo->area() << '\n';• cout << "*bar's area: " << bar->area() << '\n';• cout << "baz[0]'s area:" << baz[0].area() << '\n';• cout << "baz[1]'s area:" << baz[1].area() << '\n'; • delete bar;• delete[] baz;• system("pause");• return 0;• }

Page 24: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

expression can be read as

*x pointed to by x

&x address of x

x.y member y of object x

x->y member y of object pointed to by x

(*x).y member y of object pointed to by x (equivalent to the previous one)

x[0] first object pointed to by x

x[1] second object pointed to by x

x[n] (n+1)th object pointed to by x

This example makes use of several operators to operate on objects and pointers (operators *, &, ., ->, []). They can be interpreted as:

Page 25: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // example on this• #include <iostream>• using namespace std;

• class Dummy {• public:• int isitme (Dummy& param);• };

• int Dummy::isitme (Dummy& param)• {• if (&param == this) return true;• else return false;• }

• int main () {• Dummy a;• Dummy* b = &a;• if ( b->isitme(a) )• cout << "yes, &a is b\n";• system("pause");• return 0;• }

Page 26: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• It is also frequently used in operator= member functions that return objects by reference. Following with the examples on cartesian vector seen before, its operator= function could have been defined as:

• CVector& CVector::operator= (const CVector& param)• {• x=param.x;• y=param.y;• return *this;• }

• In fact, this function is very similar to the code that the compiler generates implicitly for this class for operator=.

Page 27: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• Static members• A class can contain static members, either data or

functions.

• A static data member of a class is also known as a "class variable", because there is only one common variable for all the objects of that same class, sharing the same value: i.e., its value is not different from one object of this class to another.

• For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:

Page 28: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // static members in classes• #include <iostream>• using namespace std;• class Dummy {• public:• static int n;• Dummy () { n++; };• ~Dummy () { n--; };• };• int Dummy::n=0;

• int main () {• Dummy a;• Dummy b[5];• Dummy * c = new Dummy;• cout << a.n << '\n';• delete c;• cout << Dummy::n << '\n';• system("pause");• return 0;• }

Page 29: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

structures• // example about structures• #include <iostream> • using namespace std;• #include <string>• struct students{• char name[50];• char city[50];• int tel;• }st1,st2;• int main()• {• //filling information• cout<<"enter student1 name, city and tel_no: ";• cin>>st1.name;• cin>>st1.city;• cin>>st1.tel;• cout<<"enter student2 name, city and tel_no: ";• cin>>st2.name;• cin>>st2.city;• cin>>st2.tel;• //printing information• cout<<"\nname"<<"\tcity\t"<<"tel_no";• cout<<"\n------------------------\n";• cout<<st1.name<<"\t"<<st1.city<<"\t"<<st1.tel<<endl;• cout<<st2.name<<"\t"<<st2.city<<"\t"<<st2.tel<<endl;• system("pause");• return 0;• }

Page 30: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• Define C++ Objects:• A class provides the blueprints for objects, so

basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box:

• Box Box1; // Declare Box1 of type Box• Box Box2; // Declare Box2 of type Box

Page 31: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream>• using namespace std;• class Box• { public:• double length; // Length of a box• double breadth; // Breadth of a box• double height; // Height of a box• };• int main( )• {• Box Box1; // Declare Box1 of type Box• Box Box2; // Declare Box2 of type Box• double volume = 0.0; // Store the volume of a box here • // box 1 specification• Box1.height = 5.0; • Box1.length = 6.0; • Box1.breadth = 7.0;• // box 2 specification• Box2.height = 10.0;• Box2.length = 12.0;• Box2.breadth = 13.0;• // volume of box 1• volume = Box1.height * Box1.length * Box1.breadth;• cout << "Volume of Box1 : " << volume <<endl;• // volume of box 2• volume = Box2.height * Box2.length * Box2.breadth;• cout << "Volume of Box2 : " << volume <<endl; • system("Pause");• return 0;• }

Page 32: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream> // class example 2 using cin• #include<conio.h>• using namespace std;• // Class Declaration• class person• {• //Access - Specifier• public:• //Varibale Declaration• string name;• int number;• };• //Main Function• int main()• {• // Object Creation For Class• person obj;

• //Get Input Values For Object Varibales• cout<<"Enter the Name :";• cin>>obj.name;

• cout<<"Enter the Number :";• cin>>obj.number;

• //Show the Output• cout << obj.name << ": " << obj.number << endl;

• getch();• return 0;• }

Page 33: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // classes example• #include <iostream>• #include<conio.h>• using namespace std;• class CRectangle {• int x, y;• public:• void set_values (int,int);• int area () {return (x*y);}• };• void CRectangle::set_values (int a, int b) {• x = a;• y = b;• }• int main () {• CRectangle rect;• rect.set_values (3,4);• cout << "area: " << rect.area();• getch(); // system pause• return 0;• }

Page 34: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

Array & Matrix

Selected topics

Page 35: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted to sort an array in an ascending order.

• 2. It will continue the above process of comparing pares, from the first pare to the last pair, at its first iteration the greatest element is placed at the last index of an array.

• 3. Then it will again repeat the above steps for all the elements by excluding the greatest one.

• 4. It will continue to repeat until the array becomes sorted.

• Let’s take a look towards the following example which will illustrate and completely describe the use, working and operations of bubble sort.

Page 36: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // sort array• #include<iostream>• #include<conio.h>• using namespace std;• main()• {• int hold;• int array[5];• cout<<"Enter 5 numbers: "<<endl;• for(int i=0; i<5; i++)• {• cin>>array[i]; • } • cout<<endl; • cout<<"Orignally entered array by the user is: "<<endl;• for(int j=0; j<5; j++)• {• cout<<array[j];• cout<<endl; • } • cout<<endl;• for(int i=0; i<4; i++)• {• for(int j=0; j<4; j++) {• if(array[j]>array[j+1]) {• hold=array[j];• array[j]=array[j+1];• array[j+1]=hold; } } } • cout<<"Sorted Array is: "<<endl;• for(int i=0; i<5; i++)• { cout<<array[i]<<endl; } • getch();• }

Page 37: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // getting number in array• #include<iostream.h>• #include<conio.h>• main()• { • int abb[4];• int r;• int initial=0;• int final=4;• int mid;• int location=-5;• cout<<"Enter 5 numbers to store in array: "<<endl;• • for(int i=0; i<5; i++)• {• cin>>abb[i]; • } • cout<<endl;• cout<<"Enter the number you want to found :";• cin>>r;• cout<<endl;• • while(initial<=final)• {• mid= (initial+final)/2;• if(abb[mid]==r)• {• location=mid;• break;• }• if(r<abb[mid])• final=mid-1;• • if(r>abb[mid])• initial=mid+1;• }• if(location==-5)• cout<<" Required number not found "<<endl;• else• cout<<" Required number is found at index "<<location<<endl; • getch();• }

Page 38: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // prime number • #include<iostream>• #include<conio.h>• using namespace std;• bool prime(int);• main()• {• int a;• cout<<"Enter an interger greater then 1: ";• cin>>a;• if(prime(a)) • cout<<a<< " is a prime Number "<< endl;• else• cout<<a<<" is not a prime Number";• getch();• }• • bool prime(int z)• {• for(int i=2; i<z; i++)• {• if(z%i==0)• return false;• • }• return true;• }

Page 39: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• //Two-Dimensional Integer Array• #include<iostream.h>• #include<conio.h> • main()• { int matrix [2][3];• // For taking integer inputs in a matrix //• for (int m1=0 ; m1<2 ; m1++)• {• for (int m2=0 ; m2<3 ; m2++)• {• cout<<"Enter Integer :";• cin>>matrix [m1][m2];• } }• cout<<endl;• • // For displaying elements of a matrix on a screen //• for (int m1=0 ; m1<2 ; m1++)• {• for (int m2=0 ; m2<3 ; m2++)• {• cout<<"Your Entered Integer are :";• cout<<matrix [m1][m2];• cout<<endl;• }• }• getch();• }

Page 40: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• //Removing element from array• #include <iostream>• #include <iomanip>• using namespace std; • void removeAt(int [], int, int);• • int main() {• • const int len = 12;

• int ylist[len] = {4, 23, 65, 34, 82, 37, 12, 17, 24, 36, 82, 51};

• int index = 1;

• removeAt(ylist, len, index);• system("pause");• • return 0; }• • void removeAt(int ylist[], int len, int index)

• { • cout << "Please pick an array index from 0 to " << (len - 1) << ": ";

• cin >> index; • • for (int loc = 0; loc < len; loc++)

• if (ylist[loc] == ylist[index])

• { • cout << endl << "The array element that you are removing is " << ylist[loc] << endl;

• }

• •

Page 41: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // counted • for (int i = index; i < len; i++)

• {

• ylist[index] = ylist[index+1];

• ylist[len-1] = 0;

• }

• • cout << "Final Array Elements are: ";

• • for (int x = 0; x < len; x++)

• {

• cout << ylist[x] << " ";

• }

• cout << endl;

• • }

Page 42: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // largest number in array • #include <iostream.h>• const int SIZE = 15;• main()• {• // Puts some numbers in the array.• int ara[SIZE]={5,2,7,8,36,4,2,86,11,43,22,12,45,6,585};• int high_val, ctr;• high_val = ara[0]; // Initializes with first• // array element.• for (ctr=1; ctr<SIZE; ctr++)• { // Stores current value if it is• // the higher than the highest.• if (ara[ctr] > high_val)• { high_val = ara[ctr]; }• }• cout << "The highest number in the list is " << high_val << "\n";• system("pause");• • return 0 ;• }

Page 43: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // Finds the highest and the lowest value in the array.• #include <iostream.h>• #include <stdlib.h>• const int SIZE = 15;• main()• {• int ara[SIZE];• int high_val, low_val, ctr;• // Fills array with random numbers from 0 to 99.• for (ctr=0; ctr<SIZE; ctr++)• { ara[ctr] = rand() % 100; }• // Prints the array to the screen.• cout << "“Here are the “ << SIZE << “ random numbers:\n";• for (ctr=0; ctr<SIZE; ctr++)• { cout << ara[ctr] << "\n"; }• cout << "\n\n"; // Prints a blank line.• high_val = ara[0]; // Initializes first element to• // both high and low.• low_val = ara[0];• for (ctr=1; ctr<SIZE; ctr++)• { // Stores current value if it is• // higher than the highest.• if (ara[ctr] > high_val)• { high_val = ara[ctr]; }• if (ara[ctr] < low_val)• { low_val = ara[ctr]; }• }• cout << "The highest number in the list is " <<high_val << "\n";• cout << "The lowest number in the list is " <<low_val << "\n";• system("pause");• return 0;• }

Page 44: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• // sort number in array• #include<iostream>• #include<conio.h>• using namespace std;• main()• { int hold;• int array[5];• cout<<"Enter 5 numbers: "<<endl;• for(int i=0; i<5; i++)• {• cin>>array[i]; • } • cout<<endl; • cout<<"Orignally entered array by the user is: "<<endl;• • for(int j=0; j<5; j++)• {• cout<<array[j];• cout<<endl; • } • cout<<endl;• for(int i=0; i<4; i++)• {• for(int j=0; j<4; j++)• {• if(array[j]>array[j+1])• {• hold=array[j];• array[j]=array[j+1];• array[j+1]=hold; • }• } • } • cout<<"Sorted Array is: "<<endl;• for(int i=0; i<5; i++)• {• cout<<array[i]<<endl; • } • • getch();• }

Page 45: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include<iostream.h> //Reverse Contents in Array

• #include<conio.h>• void reverse(int [], int);• void printarray(int [], int );• int main ()• {• const int SIZE = 10;• int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

• cout<<"Before reverse\n";• printarray(arr, SIZE);• reverse(arr, SIZE);• cout<<"After reverse\n";• printarray(arr, SIZE);• getch();• return 0;• }• void printarray(int arr[], int count)• {• for(int i = 0; i < count; ++i)• cout<<arr[i]<<' ';• cout<<'\n';• }• void reverse(int arr[], int count)• {• int temp;• for (int i = 0; i < count/2; ++i)• { temp = arr[i];• arr[i] = arr[count-i-1];• arr[count-i-1] = temp;• }• }

Page 46: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

basic file operations• // basic file operations• #include <iostream>• #include <fstream>• using namespace std;

• int main () {• ofstream myfile;

• myfile.open ("example.txt");• // myfile.open ("example.txt", ios::app);

• myfile << "Writing this to a file.\n";• myfile.close();• system("pause");• return 0;• }

Page 47: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

basic file operations• // basic cout operations• #include <iostream>• #include <iomanip>

• using namespace std;

• int main () {• double a1 = 2.0/3.0;• double a2 = 3.0/4.0;• double a3 = 8.3658;

• cout.setf(ios::fixed);• cout.setf(ios:: showpoint);• cout.precision(3);• • cout << a1 << a2 << a3 << endl;• cout << setw(16) << a1 << setw(16) << a2 << setw(16) << a3 << endl;• system("pause");

• return 0;• }

Page 48: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• //DISPLAY 6.1 Simple File Input/Output• //Reads three numbers from the file infile.dat, sums the numbers,• //and writes the sum to the file outfile.dat.• //(A better version of this program will be given in Display 6.2.)• //#include <iostream>• #include <fstream>• //#include <stdlib.h>• int main( )• { using namespace std;• ifstream in_stream;• ofstream out_stream;

• in_stream.open("infile.dat");• out_stream.open("outfile.dat");• /*• if (in_stream.fail( ))• {• cout << "Input file opening failed.\n";• exit(1);• }• */• int first, second, third;

• in_stream >> first >> second >> third;

• out_stream << "The sum of the first 3\n"• << "numbers in infile.dat\n"• << "is " << (first + second + third)• << endl;

• in_stream.close( );• out_stream.close( );• system("pause");• return 0;• }

Page 49: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

(eof)in c++• if (!(cin >> foo)) {• if (cin.eof()) {• cout << "read failed due to EOF\n";• } else {• cout << "read failed due to something other

than EOF\n"; }}• while(!file.eof())• { int i; file >> i; • doSomething(i); }

Page 50: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• using namespace std;

• int main() {• fstream inf( "ex.txt", ios::in );• while( !inf.eof() ) {• std::cout << inf.get() << "\n";• }• inf.close();• inf.clear();• inf.open( "ex.txt", ios::in );• char c;• while( inf >> c ) {• std::cout << c << "\n";• }• return 0;• }

Page 51: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• while(!inf.eof()) // EOF is false here• {• inf >> x; // read fails, EOF becomes true, x is

not set• // use x // we use x, despite our read failing.• }• However, this:

• while(inf >> x) // Attempt read into x, return false if it fails

• {• // will only be entered if read succeeded.• }

Page 52: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• class Time• {• private:• int hor ,minute ,second;• public:• void settime(int,int,int);• void printstandard ();• void printmilitary();• };

Page 53: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• class rectangle• {• int x,y;• public:• void set_value(int,int);• int area();• };• *****• class_name object_name

• rectangle rect; // rectangle calass name rect;// object_name• rectangle rect1,rect2,rect3; //many object in same class

Page 54: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream.h>• #include <cstdlib> • using namespace std; • class rectangle• {• int x ,y;• public :• void set_value(int a,int b)• {• x=a;• y=b;• }

• int area()• {• return x*y;• }• };• main()• {• rectangle rect1,rect2;• rect1.set_value(3,4);• rect2.set_value(5,6);• cout<<"Rect1 Area = "<<rect1.area()<<endl;• cout<<"Rect2 Area = "<<rect2.area()<<endl;• system("PAUSE");}

Page 55: Classes in C++ Dr. Ahmed Telba. File in C++ #include using namespace std; int main () { char filename[50]; ifstream Ahmed; cin.getline(filename,50); Ahmed.open(filename);

• #include <iostream.h>• #include <cstdlib> • using namespace std; • class circle• {• private :• int r;• float area1,c;• public :• void get_r()• {• cout<<"Enter radius \n";• cin>>r; }• void area()• {• area1=r*r*3.14; }• void cir()• {• c=2*r*3.14;• }• void display ()• {• cout<<area1<<endl;• cout<<c<<endl;• } };• main()• {• circle c;• c.get_r();• c.area();• c.cir();• c.display();• system("PAUSE"); }