Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ENG.

ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

Practicing is the only way to understand programming language.

Headlines:

1- More about initializer list.


2- const keyword.
3- Dynamic memory allocation (new /delete).
4- Stack VS. heap.
5- Copy constructor.
6- Destructor.
7- static keyword.
8- For each (range based) loop.
9- Friend function / Friend class.
10- Exercises.
11- Homework.

More about initializer list

#include <iostream>
using namespace std;
class In
{
public:
In (int a ) : n1(a) , n2(2+n1) { }
void dis( ) { cout<<"n1= "<<n1<<" , n2= "<<n2<<endl; }
private:
int n2;
int n1; }; // End of class In

2018-2019 Page 1 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

int main ( )
{
In i(12);
i.dis();
return 0;
} // End of main function

What is the output of program 1 – A?

#include <iostream>
using namespace std;
class In
{
public:
In (int a ) : n1(a) { }
void dis( ) { cout<<"n1= "<< n1<<endl; }
private:
int n1;
}; // End of class In
int main ( )
{
In i(12) ;
i.dis ( ) ;
return 0;
} // End of main function

2018-2019 Page 2 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

What will happen if:


1- Change int n1; into char n1;
2- Change cout<<"n1= "<< n1<<endl; into cout<<"n1= "<< (int)n1<<endl;
3- Change In i(12) ; into In i(300) ;
4- Change In (int a ) : n1(a) { } into In (int a ) : n1 { a } { }

int main ( )
{
char x = '1';
Don’t try this code for( ; x<=300 ; cout<<x++<<endl);
return 0;
}

const keyword

#include <iostream>
using namespace std;
class In
{
public:
int x =10 ;
void display( ) { cout<<"x = "<<x<<endl ; }
}; // End of class In
int main ( )
{ // will be written in the lab
return 0;
}

According to the information that you will learn in the lab


complete the table.

2018-2019 Page 3 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

Fill the table Object Function Correct or not


Non constant Non constant
Non constant constant
constant Non constant
constant constant

Dynamic memory allocation (new /delete)

#include <iostream>
using namespace std;
int main()
{
int *pointer ;
pointer = new int ;
cout << "The value of the pointer " <<pointer<< endl;
*pointer = 24 ;
cout << "The value of the int " <<*pointer<< endl;
delete pointer ;
return 0;
} // End of main function

What will happen if:


Adding pointer = NULL; before delete pointer ;

Don’t do this.

2018-2019 Page 4 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
int *pointer = NULL ;
cout << "Enter the number of subjects : " << endl;
int input ;
cin >> input ;
pointer = new int [ input ] ;
int temp ;
int total = 0 ;
for ( int counter = 0 ; counter < input ; counter ++)
{
cout<<"Enter degree ( "<<counter+1<< " ) "<<endl;
cin>>temp ;
*(pointer + counter ) = temp ;
total += temp ;
}
cout<<"The sum of the degrees is : "<<total<<endl;
double av = (double ) total / input ;
cout<<"The average is : "<< av <<endl;
delete [ ] pointer ;
return 0;
} // End of main function

Stack VS. heap

Will be explained in the lab.

2018-2019 Page 5 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

Copy constructor

#include <iostream>
#include<string>
using namespace std;
class Test
{
public:
Test( string n , int le )
{ name = n ; level =le ; }
void display ( )
{ cout<<"Student name: " <<name<< endl;
cout<<"Level: "<<level<<endl;
}
void setNL ( string nn , int lee )
{
name = nn ;
level = lee ;
}
private:
string name;
int level;
}; // End of Test class
int main ( )
{
Test t1("Elham" , 3);
t1.display( );
Test t2 =t1;
t2.display( );
t1.setNL("Aya" , 1);
t1.display();
t2.display();
return 0;
} // End of main function

2018-2019 Page 6 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

#include <iostream>
#include<string>
using namespace std;
class Test
{
public: What will happen if we use
Test( string n , int le )
dynamic allocation memory in
{ name = new string (n);
program 4-A.
level =le ;
}
void display ( )
{ cout<<"Student name: " <<*name<< endl;
cout<<"Level: "<<level<<endl;
}
void setNL ( string nn , int lee )
{
*name = nn ; level = lee ; }
private:
string *name;
int level;
}; // End of Test class
int main ( )
{
Test t1("Elham" , 3);
t1.display( );
Test t2 =t1;
t2.display( );
t1.setNL("Aya" , 1);
t1.display();
t2.display();
return 0;
} // End of main function

2018-2019 Page 7 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

To solve this problem, we have to use copy constructor.

Add this to the class Test( Test &cobj )


{ name = new string (*cobj.name);
level = cobj.level; }

What will happen if we delete the


following line:
level = cobj.level;

Destructor

#include <iostream>
using namespace std;
class Test
{ public:
Test( ) {cout<<"Constructor -> Lab 4 in c++"<<endl;}
Test (int a)
{ num = a;
cout<<"Constructor -> your level is "<<num<<endl;
}
~Test ( ){ cout<<"Destructor is called / num ="<<num<<endl;}
private:
int num = 0 ;
}; // End of class Test
int main( )
{
// will be written in the lab
return 0;
} // End of main function

2018-2019 Page 8 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

What will happen if we add the following:


~Test( int a){cout<<"Destructor"<<endl;}

Static keyword

#include <iostream>
using namespace std;
int stest( );
int main( )
Static local variable

{
int result = 0;
for (int i =0 ; i++ <= 3 ; result = stest( ))
{ cout<<"The result is "<<result <<endl ; }
return 0 ;
} // End of main function
int stest ( ) What is the output when we
{ delete static keyword from
static int x =2; the program?
x+=2;
return x ;
} // End of stest function

#include <iostream>
Static local variable

using namespace std;


class StaticTest
{
public:
static int c ;
StaticTest ( ) { a++ ; b++ ; c++; }
void display( ) {
cout<<a <<" "<<b<<" "<<c<<endl;
}
private:

2018-2019 Page 9 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

int a =1 ;
Static data member

static int b ; };
// End of StaticTest class
int StaticTest::b=20;
int StaticTest::c=40;
int main( )
{
// will be written in the lab
return 0;
} // End of main function

Test static function by yourself

For each (range based) loop

#include <iostream>
using namespace std;
int main ( )
{
int a [ ] {12 ,8 ,4, 6,10} ;
for ( int i = 0 ; i < 5 ; i++)
{cout<<a[i]<<"\t"; }
cout<<endl<<"------------------------------"<<endl;
for( int e : a )
{ cout<<e<<"\t"; }
return 0; For each -> c++ 11
} // End of main function

By using for loop and for each loop:


- Increase the elements of a by one the print the
result.

2018-2019 Page 10 of 11
ENG. ELHAM ABDULRAHMAN C++ LAB – MECHATRONICS – LEVEL 3

Friend function / Friend class

#include <iostream>
using namespace std;
class FTest
{
public:
void dis1 ( ){ cout<<a<<" , "<<b<<endl; }
private:
int a = 2 ; int b = 4 ;
}; 1- Add the following line to the class:
void dis2 ( ){
friend void dis2( );
FTest ft;
2- Try the friend class by yourself.
ft.a =10 ;
ft.b =30;
cout<<ft.a<<" , "<<ft.b<<endl; }
int main ( )
{
FTest ft;
ft.dis1();
dis2();
return 0;
} // End of main function

Exercises

Homework

Will be taken in
the lab

2018-2019 Page 11 of 11

You might also like