Applying Object-Oriented Programming Language Skills
Applying Object-Oriented Programming Language Skills
DATABASE ADMINISTRATION
LEVEL – IV
Operators in C++
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
C++ is rich in built-in operators and provides following type of operators:
• Arithmetic Operators Scope resolution operator
• Relational Operators Logical Operators
• Increment Operator Assignment Operators
• Decrement Operator Arithmetic Operators:
The following arithmetic operators are supported by C++ language:
Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by de-numerator B / A will give 2
Modulus Operator and remainder of after an integer
% B % A will give 0
division
++ Increment operator, increases integer value by one A++ will give 11 -- Decrement
operator, decreases integer value by one A-- will give 9
Relational Operators:
The following relational operators are supported by C++ language:
Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example
== Checks if the value of two operands is equal or not, (A == B) is not true.
!= Checks if the value of two operands is equal or not, (A! = B) is true.
Checks if the value of left operand is greater than
> (A > B) is not true.
the value of right operand,
Checks if the value of left operand is less than the
< (A < B) is true.
value of right operand,
Checks if the value of left operand is greater than or
>= (A >= B) is not true.
equal to the value of right operand,
Checks if the value of left operand is less than or
<= (A <= B) is true.
equal to the value of right operand,
Logical/Boolean operator:
The following logical operators are supported by C++ language: Assume
variable A holds 1 and variable B holds 0 then:
Operator Description Example
If both the operands are non-zero then condition
&& (A && B) is false.
becomes true.
If any of the two operands is non-zero then
|| (A || B) is true.
condition becomes true.
Use to reverses the logical state of its operand. If a
! condition is true then Logical NOT operator will !(A && B) is true. make
false.
Assignment Operators:
Assignment operator is used for assign/initialize a value to the variable during the program execution. The
following assignment operators are supported by C++ language:
Operator Description Example
Simple assignment operator, Assigns values from
= C = A + B will assign value of A + B into C
right side operands to left side operand
Add AND assignment operator, It adds right
+= operand to the left operand and assign the result to C += A is equivalent to C = C + A
left operand
Subtract AND assignment operator, It subtracts
-= right operand from the left operand and assign the C -= A is equivalent to C = C - A
result to left operand
Multiply AND assignment operator, It multiplies
*= right operand with the left operand and assign the C *= A is equivalent to C = C * A
result to left operand
Divide AND assignment operator, It divides left
/= operand with the right operand and assign the result C /= A is equivalent to C = C / A to
left operand
Modulus AND assignment operator, It takes
%= modulus using two operands and assign the result to C %= A is equivalent to C = C % A
left operand
Increment Operator (++): This operator is used for increment the value of an operand. Example:
assume that A=20 and B=12, then ++A=21, ++B=13 and A++=20, B++=12
Decrement Operators (--):This operator is used for decrement the value of an operand.
Example: assume that B=14 and C=10, then –B=13, --C=9 and B-- =14, C--=10
Scope resolution operator (::)
This operator is used to differentiate a local variable from the global variable. The variable having the same name
can have different meaning at different block. Expression:
Expressions are formed by combining operators and operands together following the programming language.
Compile & Execute C++ Program:
Lets look at how to save the file, compile and run the program. Please follow the steps given below:
• Open a text editor and write the code Debug the code.
• Save the file as : hello.cpp You will be able to see ' Hello World ' printed Compile it to check
if it has error on the window.
Semicolons & Blocks in C++:
In C++, the semicolon is a statement terminator. Each individual statement must be ended with a semicolon.
For example, following are three different statements: x = y;
y = y+1;
add(x, y);
A block is a set of logically connected statements that are surrounded by opening and closing braces. For example:
{
cout << "Hello World"; // prints Hello World
return 0;
}
1.3. Using the Appropriate Language Syntax for Sequence, Selection and Iteration Constructs
Control Structures in C++ is a Statement that used to control the flow of execution in a program. There
are three types of Control Structures:
1. Sequence structure
2. Selection structure
3. Loops/ Repetition/ Iteration Sequence Structure in C++
The sequence structure is built into C++ statements that execute one after the other in the order in which they are
written—that is, in sequence. Break and continue statements are example of sequence control structure.
The ‗break‘ statement causes an immediate exit from the inner most ‗while‘, ‗do…while‘, ‗for loop‘ or from a
‗switch- case‘ statement.
If you are using nested loops (ie. one loop inside another loop), the break statement will stop the execution of the
innermost loop and start executing the next line of code after the block. Flow Diagram:
Example:
#include <iostream.h>
int main ()
{
int a = 10; // Local variable declaration:
do{ // do loop execution
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
{
break;
}
}while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces following result:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
The goto statement provides an unconditional jump from the goto to a labeled statement in the same function.
Syntax of a goto statement in C++:
goto label;
..
.
label: statement;
Where label is an identifier that identifies a labeled statement. A labeled statement is any statement that is preceded
by an identifier followed by a colon (:).
Flow Diagram:
Example:
#include <iostream.h>
int main ()
{
int a = 10; // Local variable declaration:
return 0;
}
When the above code is compiled and executed, it produces following result:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16
value of a: 17 value of a: 18 value of a: 19
The continue statement is similar to ‗break‘ statement but instead of terminating the loop, the continue statement
returns the loop execution if the test condition is satisfied.
Flow Diagram:
Example:
#include <iostream.h>
int main ()
{
int a = 10; // Local variable declaration:
return 0;
}
When the above code is compiled and executed, it produces following result:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16
value of a: 17 value of a: 18 value of a: 19
Selection statements in C++
There are basically two types of control statements in c++ that allows the programmer to modify the regular
sequential execution of statements. They are selection and iteration statements.
The selection statements allow to choose a set of statements for execution depending on a condition. If
statement and switch statement are the two selection statements.
If ... else statement:
Syntax: if (expression or condition)
{
Statement 1;
Statement 2;
} Else
{
Statement 3;
Statement 4;
}
If the condition is true, statement1 and statement2 is executed; otherwise statement 3 and statement 4 is executed. The
expression or condition is any expression built using relational operators which either yields true or false condition.
Flow Diagram:
If the condition evaluates to true, then the if block of code will be executed otherwise else block of code will be
executed. Following program implements the if statement.
# include <iostream.h> void main()
{
int num;
cout<<"Please enter a number"<<endl;
cin>>num; if ((num%2) == 0)
cout<<num <<" is a even number"; else
cout<<num <<" is a odd number";
}
The above program accepts a number from the user and divides it by 2 and if the remainder (remainder is obtained by
modulus operator) is zero, it displays the number is even, otherwise it is odd. you must use the relational operator ‗
==‘ to compare whether remainder is equal to zero or not.
Switch statement
One alternative to nested if statement is the switch statement which allows a variable to be tested for equality against
a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax:
Switch (variablename)
{
case value1: statement1; break; case value2:
statement2; break; case value3: statement3; break;
default: statement4;
}
Flow Diagram:
Example: #include<iostream.h>
void main() {
int choice;
cout<< ―Enter a value for choice \n‖;
cin >> choice;
switch (choice)
{
case 1: cout << "First item selected!" << endl;
break;
case 2: cout << "Second item selected!" << endl;
break;
case 3: cout << "Third item selected!" << endl;
break;
default: cout << "Invalid selection!" << endl;
}
}
Iteration statements in C++
Iteration or loops statements are important statements in c++, which helps to accomplish repeatitive execution of
programming statements.
There are three loop statements in C++. They are - while loop, do while loop and for loop.
While Loop
The while loop construct is a way of repeating loop body over and over again while a certain condition remains true.
Once the condition becomes false, the control comes out of the loop. Loop body will execute only if condition is true.
Syntax: While (condition or expression)
{
Statement1;
Statement 2;
}
Flow Diagram:
The flow diagram indicates that a condition is first evaluated. If the condition is true, the loop body is executed and
the condition is re-evaluated. Hence, the loop body is executed repeatedly as long as the condition remains true. As
soon as the condition becomes false, it comes out of the loop and goes to display the output. Example:
#include<iostream.h>
void main()
{ int n, i=1, sum=0;
cout<< "Enter a value for n \n";
cin>>n;
while(i<=n)
{
sum=sum+i;
i++;
}
cout<< "sum of the series is "<<sum;
}
Do …While loop
The do... while statement is the same as while loop except that the condition is checked after the execution of
statements in the do..while loop.
Syntax: do{
Statement;
While (test condition);
Statement;
} Flow
Diagram:
Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after
the execution of statement. Hence in do..while loop, Loop body execute once even if the condition is false.
Example: #include<iostream.h>
void main()
{
int n, i=1, sum=0;
cout<< "Enter a value for n \n";
cin>>n;
do{
sum=sum+i;
i++;
} while(i<=n);
cout<< "sum of the series is "<<sum;
}
For loop:
The for loop statements (loops or iteration statement) in C++ allow a program to execute a single statement multiple
times (repeatedly) , given the initial value, the condition, and increment/decrement value. syntax: for ( initial value ;
test condition ; increment/decrement)
{
Statement;
}
Flow Diagram:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific
number of times.
Example: #include<iostream.h>
void main()
{ int i, n, sum=0;
cout<< "Enter the value for n";
cin>>n;
for(i=1;i<=n; i++)
{
sum=sum+i;
} cout<<
"the sum is" <<sum; }
1.4. Using modular programming approach
Many programs are too long or complex to write as a single unit. Programming becomes much simpler when the code
is divided into small functional units (modules).
Modular programming is a programming style that breaks down program functions into modules, each of which
accomplishes one function and contains all the source code and variables needed to accomplish that function.
Modular programs are usually easier to code, compile, debug, and change than large and complex programs.
The benefits of modular programming are:
• Efficient Program Development
Programs can be developed more quickly with the modular approach since small subprograms are easier to
understand, design, and test than large and complex programs.
• Multiple Use of Subprograms
Code written for one program is often useful in others.
• Ease of Debugging and Modifying
Modular programs is generally easier to compile and debug than monolithic (large and complex) programs. 1.5.
Using arrays and arrays of objects
An Array is a collection of similar data items which shares a common name within the consecutive memory. An
Array can be any data type, but it should be the collection of similar items.
Each item in an array is termed as ‗element‘, but each element in an array can be accessed individually.
The number of element in an array must be declared clearly in the definition.
The size or number of elements in the array can be varied according to the user needs.
Syntax for array declaration:
DataType ArrayName [number of element in the array]
Example: int a[5]; - ‗int‘ is the data type.
Namespace
Namespaces are used in the visual C++ programming language to create a separate region for a group of variables,
functions and classes etc. Namespaces are needed because there can be many functions, variables for classes in one
program and they can conflict with the existing names of variables, functions and classes. Therefore, you may use
namespace to avoid the conflicts.
A namespace definition begins with the keyword namespace followed by the namespace name as shown bellow.
namespace namespace_name
{
// code declarations
}
Let us see how namespace scope the entities including variable and functions:
#include <iostream>
using namespace std; //
first name space namespace
first_space{ void
func(){
cout << "Inside first_space" << endl;
}
}
public:
int cube()
{
return (val*val*val);
}
};
int main ()
{
Cube cub;
cub.set_values (5);
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}
2. Multiple Inheritances: - If a class is derived from more than one base class, it is known as multiple inheritances.
Example: #include<iostream.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks:";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement: public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No :"<<rno<<"\n\tTotal : "<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
}
3. Multilevel Inheritance:
When a derived class is created from another derived class, then that inheritance is called as multi level inheritance.
Example: #include <iostream.h>
class A
{
protected: int
rollno; public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:\n"<< rollno << "\n";
}
};
class marks : public B
{
protected:
int sub1; int
sub2; public:
void get_marks(int x,int y)
{
sub1 = x; sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class C : public marks
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2;
put_num();
put_marks();
cout << "Total:"<< tot;
}
};
int main()
{
C std1;
std1.get_num(5);
std1.get_marks(10,20);
std1.disp();
return 0;
}
Result:
Roll Number Is:5
Subject 1: 10
Subject 2: 20
Total: 30
4. Hierarchical Inheritance:
If a number of classes are derived from a single base class, it is called as hierarchical inheritance.
This means a Base Class will have Many Sub Classes or a Base Class will be inherited by many Sub Classes.
Example:
Example: #include<iostream.h>
Class A
{
int a,b;
public :
void getdata()
{
cout<<"\n Enter the value of a and b";
cin>>a>>b;
} void
putdata()
{
cout<<"\n The value of a is :"<<a "and b is "<<b;
}
};
class B : public A
{
int c,d;
public :
void intdata()
{
cout<<"\n Enter the value of c and d ";
cin>>c>>d;
}
void outdata()
{
cout<<"\n The value of c"<<c"and d is"<<d;
}
};
class C: public A
{
int e,f;
public : void
input() {
cout<<"\n Enter the value of e and f";
cin>>e;>>f
}
void output()
{
cout<<"\nthe value of e is"<<e"and f is" <<f;
}
void main() {
B obj1 C obj2;
obj1.getdata(); //member function of class A
obj1.indata(); //member function of class B obj2.getdata();
//member function of class A obj2.input(); //member function of
class C obj1.putdata(); //member function of class A
obj1.outdata(); //member function of class B obj2.output();
//member function of class A
obj2.outdata(); //member function of class C
}
5. Hybrid Inheritance: This is a Mixture of two or More Inheritance types.
I.e.: Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.
Example: #include <iostream.h>
class A {
protected:
int rollno; public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:"<< rollno << "\n"; }
};
class B : public A
{
protected:
int sub1;
int sub2; public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class C
{
protected:
float e; public:
void get_extra(float s)
{
e=s;
}
void put_extra(void)
{
cout << "Extra Score::" << e << "\n";}
};
return 0;
}
2.7. Encapsulation
Encapsulation is the method of combining the data and functions inside a class. This hides the data from being
accessed from outside a class directly, only through the functions inside the class is able to access the information.
Encapsulation is the term given to the process of hiding all the details of an object that do not necessary to
its user.
Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.
Features and Advantages of the concept of Encapsulation: -
Makes Maintenance of Application Easier
- Improves the Understandability of the Application
- Enhanced Security
- Protection of data from accidental corruption
- Flexibility and extensibility of the code and reduction in complexity
LO3. Debug code
3.1. Using an Integrated Development Environment
An integrated development environment (IDE) is a software application that provides comprehensive facilities to
computer programmers for software development. An IDE normally consists of a source code editor, build
automation tools,compiler or interpreter and a debugger.
Ie: An integrated development environment (IDE) is a programming environment that consisting of a code editor, a
compiler, a debugger, and a graphical user interface (GUI) builder.
3.2. The Language Debugging Facilities
This topic describes methods of debugging routines in Language Environment. Debug tools are designed to help you
to detect errors early in your routine.
Debug Tool also provides facilities for setting breakpoints and altering the contents and values of variables.
3.2.1. Visual C++, C#, ASP.Net
Visual C++ program is an application development tool developed by Microsoft for C++ programmers that supports
object-oriented programming with an integrated development environment (IDE).
3.2.2. Visual Studio suite
What Is Visual Studio?
The Microsoft Visual Studio development system is a suite of development tools designed to aid software developers.
It is used to develop graphical user interface applications along with Windows Forms applications, web sites, web
applications, and web services.
System Requirement to install Visual Studio.net 2008:
Processor Minimum 1.6 GHz CPU, recommended 2.2 GHz or higher CPU
Hardisk Space Minimum 5400 RPM hard disk, recommended 7200 RPM or higher hard disk
Supporting Operating System Microsoft Windows XP , Microsoft Windows Server 2003 , Windows Vista, or Latest version
The installation starts. Just wait and see the step by step, visual studio 2008 components being installed.
In the welcome setup wizard page you can enable the tick box to send your setup experience to Microsoft if you want
.in this case we just leave it unchecked. Just wait for the wizard to load the installation components.
Click the next button to go to the next step
Any component that failed to be installed will be marked with the Red Cross mark instead of the green tick for the
successful. In this case we just exit the setup wizard by clicking the Finish button.
The setup wizard will list down all the required components need to be installed. Notice that visual studio 2008 needs
.Net Framework version 3.5. Then click the next button.
In the installation type, there are three choices: default, full or custom. In our case, select the full and click the install
button. Full installation required around 4.3GB of space
Click Restart Now to restart you machine.
The Windows Start menu for Visual Studio 2008 is shown below.
Depending on your programming needs, you will select one of the visual studio component Settings.
The Visual Studio 2008 is configuring the development environments to the chosen one for the first time use.
Create a Project in VB.NET
A Visual Basic Project is container for the forms, methods, classes, controls that are used for a Visual
Basic
Application.
Steps to Create a VB.net Project:
1. Click on the Programs->Microsoft Visual Studio.net 2008.
2. Choose File -->New Project from the Menu Bar to get the New Project window.
. Open
7By Existing
default, a projectProject
named Myin VB.NET
Project and a form Form1 will be created. Project name and form name can be
In Visual
renamed Studio.net 2008, an existing project can be opened using the Recent Projects option in the Start Page
later.
or can be opened using the File -> Open Project from the Menu Bar. Both these displays a window with project
folder, once the project is selected and opened using the file with the extension .sln for windows application, the
Solution Browser displays all the components of that project.
Button Description Adds a new Project.
Menu Bar
Menu bar in Visual Basic.net 2008 consist of the commands that are used for constructing a software code. These
commands are listed as menus and sub menus.
Menu bar also includes a simple Toolbar, which lists the commonly used commands as buttons. This Toolbar can be
customized, so that the buttons can be added as required.
Following table lists the Toolbars Buttons and their actions.
Open a New Window. Undo.
Open a File. Redo.
Saves the Current Form.
Continue Debugging.
Saves all files related to a
project. Break Debugging.
Properties Window
Windows form properties in Visual Basic.net 2008 lists the properties of a selected object. Every object in VB
has it own properties that can be used to change the look and even the functionality of the object.
Properties Window lists the properties of the forms and controls in an alphabetical order by default.
A form is created by default when a Project is created with a default name Form1. Every form has its own
Properties, Methods and Events. Usually the form properties name, caption are changed as required, since multiple
forms will be used in a Project.
Form Properties
Name of trainer: Sisay Date: ____/____/04
Name of trainer: Sisay Date: ____/____/04
Name of trainer: Sisay Date: ____/____/04
Visual Basic.net 2008 combined under a single user defined data type. This gives the flexibility to inlcude real
time objects into an application.
User defined data types are defined using a Structure Statement and are declared using the Dim Statement.
Structure Statement:
The Structure statement is declared in the general declaration part of form or a Module. Syntax:
[ Public | Protected | Private]
Structure varname elementname [([subscripts])] As type
[elementname [([subscripts])] As type]
...
End Structure
In the above syntax varname is the name of the user defined data type, that follows the naming convention of a
variable. Public option makes these datatypes available in all projects, modules, classes. Type is the primitive
datatype available in visual basic.
Dim Statement:
This is used to declare and allocate a storage space for a variable or an user defined variable.
Syntax: Dim variable [As Type] Example:
Structure EmpDetails
Dim EmpNo As Integer
Dim EmpName As String
Dim EmpSalary As Integer
Dim EmpExp As Integer
End Structure
Public Class Form1
Private Sub Button1_Click ByVal sender As System.Object,ByVal e As System.EventArgs)
Handles Button1.Click
Dim TotalSal As New EmpDetails()
TotalSal.EmpNo = TextBox1.Text
TotalSal.EmpName = TextBox2.Text
TotalSal.EmpSalary = TextBox3.Text
TotalSal.EmpExp = TextBox4.Text
TextBox5.Text = Val(TotalSal.EmpSalary) * Val(TotalSal.EmpExp)
End Sub
End Class
If Then Statement
If Then statement is a control structure which executes a set of code only when the given condition is true.
Syntax:
If [Condition] Then
[Statements]
In the above syntax, when the Condition is true then the Statements after Then are executed.
Example:
Private Sub Button1_Click_1(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) > 25 Then
TextBox2.Text = "Eligible"
End If
If Then Else Statement
If Then Else statement is a control structure which executes different set of code statements when the given
condition is true or false.
Do … while code
Do
statement-block
Loop While condition
Do
Name of trainer: Sisay Date: ____/____/04
statement-block
Loop Until condition
Example: find the factorial of a number n inputted from the keyboard using do…while or do …until loop.
Dim fact, i As Integer
fact = 1
i = 1
Do
fact = fact * i
i = i + 1
Loop While (i <= Val(TextBox1.Text))
TextBox2.Text = fact
Or
Dim fact, i As Integer
fact = 1
i = 1
Do
fact = fact * i
i = i + 1
Loop until(i > Val(TextBox1.Text))
TextBox2.Text = fact
Steps to create a connection to SQL Database VB.Net
1. Using wizard:
Once you have your VB software open, do the following:
Click File > New Project from the menu bar
Select Windows Application, and then give it the Name. Click OK
Locate the Solution Explorer on the right hand side.
We need to select Show Data Source from data on the menu bar. Then, click on Add New Data Source.
When you click on the link Add a New Data Source, you will see a screen shown bellow. Then select Database and
click next.
Write the server name and your Database name. Then click on Test Connection to check the connection.
Click on Next.
Here, you can select which tables and fields you want. Tick the Tables box to include them all. You can give your
DataSet a name, if you prefer. Click Finish and you're done.
The Data Sources area of the Solution Explorer (or Data Sources tab on the left) now displays information about
your database. Click the plus symbol next to tblContacts:
All the Fields in the Address Book database are now showing.
To add a Field to your Form, click on one in the list. Hold down your left mouse button, and drag it over to your
form:
Click the Navigation icons to move backwards and forwards through your database.