UNIT FIVE
FUNDAMENTALS OF PROGRAMMING
Content
• File Handling:
• Declaration of files,
• types of files File pointer.
• File input/ output and usage,
• File operation
• Introduction to Object Oriented Programming:
• OOPS concepts,
• OOP languages- C++, Python etc.
File Handling
• In C, we can create, open, read, and write to files
by declaring a pointer of type FILE, and use the
fopen() function:
• FILE *fptr
fptr = fopen(filename, mode);
Con’t…
• To create a file, you can use the w mode inside
the fopen() function.
Create a
File • The w mode is used to write to a file. However,
if the file does not exist, it will create one for
you:
Example
int main() {
FILE *fptr;
// Create a file
fptr = fopen("filename.txt", "w");
// Close the file
fclose(fptr);
return 0; // Return 0 to indicate success
}
To create file in specific folder, use absolute path as:
fptr = fopen("C:\directoryname\filename.txt", "w");
We can use the w mode from the previous
example and write something to it.
Write To a File The w mode means that the file is opened
for writing.
To insert content to it, you can use the
fprintf() function and add the pointer
variable (fptr in our example) and some text
Example
int main() {
FILE *fptr;
// Create a file
fptr = fopen("filename.txt", "w");
// Write some text to the file
fprintf(fptr, “Adding this text to filename.txt");
// Close the file
fclose(fptr);
return 0; // Return 0 to indicate success
}
• If you write to a file that already
exists, the old content is deleted,
Important Point
and the new content is inserted.
This is important to know, as you
might accidentally erase existing
content.
Append Content To a File
If you want to add content to a file without deleting the old
content, you can use the a mode.
The a mode appends content at the end of the file:
Example
int main() {
FILE *fptr;
// Create a file
fptr = fopen("filenamef.txt", "a");
// Write some text to the file
fprintf(fptr, “\nAppend this");
// Close the file
fclose(fptr);
return 0; // Return 0 to indicate success
}
Important Point
• Just like with the w mode; if the file does not exist, the a mode will
create a new file with the "appended" content.
Read a File
In the previous example, we To read from a file, In order to read the content The fgets() function takes fgets(myString, 100, fptr);
wrote to a file using w and a of filename.txt, we can use three parameters:
modes inside the fopen() we can use the r the fgets() function.
function. mode:
Con’t…
The first parameter specifies where to store the file content,
which will be in the myString array we just created.
The second parameter specifies the maximum size of data to
read, which should match the size of myString (100).
The third parameter requires a file pointer that is used to
read the file (fptr in our example).
Example
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("filename.txt", "r");
// Store the content of the file
char myString[100];
// Read the content and store it inside myString
fgets(myString, 100, fptr);
// Print file content
printf("%s", myString);
// Close the file
fclose(fptr);
return 0;
}
Important Point
• The fgets function only reads the first line of the file.
• To read every line of the file, we can use a while loop:
Example
• int main() {
• FILE *fptr;
• // Open a file in read mode
• fptr = fopen("filename.txt", "r");
•
// Store the content of the file
• char myString[100];
•
// Read the content and print it
• while(fgets(myString, 100, fptr)) {
• printf("%s", myString);
• }
•
// Close the file
• fclose(fptr);
•
return 0;
• }
Object Oriented and
Procedural Programming
Write once run anywhere!
Object Oriented Concepts
• To develop an application we require a programming language which
uses one of the following concepts:
a. Procedural Oriented Programming Concepts
b. Object Oriented Programming Concepts
Procedural Oriented Programming Concepts
• A language is said to be procedural oriented if the applications are
developed with the help of procedures or functions.
•
• Examples of Procedural Oriented Programming concepts are:
•
• C Language , COBAL , FORTRAN , PASCAL
Limitations of Procedural Programming
▪ The applications which are develop by using procedural oriented programming languages
are difficult to maintain and debugging of such applications are time consuming.
▪ The applications that are develop by using procedural oriented programming languages
do not provide full security to the data.
▪ The applications that are develop by using procedural oriented programming concepts
give more importance to the functions rather than data, the data available in the
applications that are develop by using procedural oriented programming concepts are
open and therefore they are not suitable for developing distributed applications.
Continue…
▪ The applications which are developed by using procedural oriented programming
concepts are difficult to enhance that is, it does not support the integration of new
modules.
▪ The procedure and functions are the fundamental concepts of procedural oriented
programming concepts, the design of these fundamental concepts are very weak
therefore they are not suitable to develop real time and complex applications.
Note:
• The procedural oriented languages are also called as structured programming language.
• Object Oriented Programming Concepts
• The applications which are develop with the help of objects and classes are said to
follow object oriented programming concepts.
•
Example of Languages using object oriented concepts are:
• Simula, C++ , JAVA , .NET , RUBBY , Small talk , LISP, Python
• Even though C++ as an object oriented programming language , but
according to programming language experts it is called as partial
object oriented language because of the following reasons:
Continue…
▪ In a C++ application we can write some code inside the class and some code
outside the class.
▪ An application in C++ can be develop without following any object oriented
programming concepts.
▪ The friend function concept in C++ can violate (break) all the security provide for
the data, and accessing it.
Continue…
• The object and classes are the fundamental concepts of object
oriented programming.
Object
a) Any entity that exist physically in the real world , which requires
memory called as object.
b) Every object will contain some properties and actions.
c) The properties are data or information which describe the object
and they are represented by variable.
d) The actions are the task or the operations performed by objects and
they represent by methods.
Class
▪ A class is a collection of common properties and common actions of a group of objects.
▪ A class can be considered as a plan, blueprint or model for creating the objects.
▪ For every class we can create (n) numbers of objects. And without a class object creation
is not possible.
▪ An object is an instance of a class.
Con’t…
▪ All the object oriented programming concepts are derived from the real world,
from human being lives so that programming becomes so simpler.
▪ The programmer can understand the concepts easily and implement them
without any difficulty.
▪ The design of the object oriented programming concept is very strong, and they
are suitable for developing real time and complex applications.
The various object oriented programming concepts are:
1. Encapsulation
2. Inheritance
3. Polymorphism
Encapsulation
It is the process of bindings the variables and methods into a single entity.
• Encapsulation can be achieved with the help of a class.
• Using encapsulation we can improve the maintenance of the applications.
• Using encapsulation we can achieve data hiding (if we have a class) using
encapsulation we can implement abstraction.
Abstraction means
• (which will decide what to hide and what to presents that is we can implement security.
• Using encapsulation we can isolate or separate the members from one class to another
class and thereby reducing the debugging time.
• For example we are creating an application for a bank, a bank has employee and
customers so we can create separate class for employee details like ID, Name and
separate class for its customers as per below:
Example
• public class Customer{
•
• int ID;
• String name;
• }
•
• public class Employee{
• int ID;
• String name;
• }
Inheritance
• It is a process of acquiring the members from one class to another class.
• Using inheritance we can achieve reusability and thereby reducing the code
size and development time of the application.
• Like:
• In a company if we develop an application for rate of interest as rate of
interests are different so if one programmer develop one structure or
application for car loan we can use it for gold loan, house loan and etc.
Continue…
• Or Microsoft has developed Windows XP supposed in 5 years and after 2 years wanted to develop
Window Vista now he doesn’t require 5 years more to develop Windows Vista he can use the
concepts of Inheritance and inherit all the properties, functions and methods which were used for
developing Windows XP and only add some new updates to it and change the name from XP to
Vista so now it will only 5-8 months approximately.
• It should not be left unsaid that the concept of inheritance plays key role in technology world.
Polymorphism
• If a single entity shows multiple behaviours or forms then that is said
to be polymorphism using polymorphism we can achieve flexibility
where a single entity can perform different operations according to
the requirement.
Class
Using a class we can achieve encapsulation.
• Using a class we can create user defined data type where we can store (n)
numbers of values and any type of values, according to the application
requirements a class can contain variables and methods which are together called
as members of the class.
• The Java program can contain (n) numbers of classes.
How to write a class in Java.
•
• Syntax:
• Class className{
• Datatype variable1;
• Datatype variable2;
• Datatype variable3;
• :
• :
• Return type methodName1(parameters)
• {
• Statements;
• }
• Return type methodName2(parameters)
• {
• Statements;
• }
• :
• :
• }
Example
class Student{
• int rollName;
• double marks;
• String name;
• void display(){
• System.out.println("Roll No: " +rollName);
• System.out.println("Mark: " +marks);
• System.out.println("Name: " +name);
• }
• public static void main(String args[]){
• System.out.println("Student Information");
• Student st = new Student();
•
• st.display();
• }
•}
File Name Student.Java
• When the above Java program Student.Java is compiled the compiler will verify
whether the Java code available in the program is valid or not, if valid the
compiler generates Student.class.
• The .class file generated by the compiler will be provided to the JVM for
execution.
• The execution of the Java program will be done by JVM and it begins from the
main method.
• Initially the Java stack will be empty.