Beruflich Dokumente
Kultur Dokumente
• Computer software
– Is the product that software engineers design and build.
– It encompasses
• Programs that execute with in a computer of any size and architecture.
• Documents that encompass hard copy and virtual forms.
• Data that combine numbers and text but also includes representations of pictorial, video and
audio information.
• Programming Language
– Like any human language, a programming language provides a way to express concepts.
– Program development involves creating models of real world situations and building computer
programs based on these models.
– Computer programs describe the method of implementing the model.
– Computer programs may contain computer world representations of the things that constitute the
solutions of real world problems
• Procedural programming (structural programming)
– Pascal, C, BASIC, Fortran, and similar traditional programming languages are
procedural languages. That is, each statement in the language tells the
computer to do something.
– Uses structural constructs like decision making (if else, while), looping,
functions.
– In a procedural language, the emphasis is on doing things (functions).
– A program is divided into functions and—ideally, at least— each function has
a clearly defined purpose and a clearly defined interface to the other
functions in the program.
#include<stdio.h>
int main() {
int a, b, c;
scanf("%d%d",&a,&b);
c = a + b;
return 0;
}
#include<stdio.h>
1
2
int main() {
3
4
int num1, num2, res;
5
6
printf("\nEnter the two numbers : ");
7
scanf("%d %d", &num1, &num2);
8
9
//Call Function Sum With Two Parameters
10
res = sum(num1, num2);
11
12
printf(“\nAddition of two number is : ");
13
return (0);
14
}
15
16
int sum(int num1, int num2) {
17
int num3;
18
num3 = num1 + num2;
19
return (num3);
20
}
• Struct DATE {
int date;
int month;
int year;
};
Class DATE {
Private:
int date, month, year;
Public:
void set_date(int d, int m, int y);
void show_date(int d, int m, int y);
};
DATE d1;
• Here we are defining a variable date d1. here d1 is an object. The object
and functions set_date() and show_date() are tightly coupled. Once this
class is fully tested, it can be easily reused in any new application.
• You can modify date using only set_date() function and see or print date
only with help of show_date().
• OVERVIEW of Object Oriented Programming Language
– The fundamental idea behind object-oriented programming is:
– The real world consists of objects. Computer programs may contain
computer world representations of the things (objects) that constitute
the solutions of real world problems.
– Real world objects have two parts:
• Attributes (or state: characteristics that can change),
• functionality (or abilities: things they can do).
– To solve a programming problem in an object-oriented language, the
programmer no longer asks how the problem will be divided into
functions, but how it will be divided into objects.
– The emphasis is on data
– What kinds of things become objects in object-oriented programs?
• Human entities: Employees, customers, salespeople, worker, manager
• Graphics program: Point, line, square, circle, ...
• Mathematics: Complex numbers, matrix
• Computer user environment: Windows, menus, buttons
• Data-storage constructs: Customized arrays, stacks,
• Features of object oriented language
– Encapsulation: in above example we are putting three variables and two functions into a
bundle. there is an invisible shell around this data. In other word we are encapsulating the
data. It is more secure. You cannot use a function without its corresponding object. This adds
much safety to the code.
– Data hiding: you cannot tamper date, month or year. They are actually hidden from your
program (main and other functions). This is the principle of data hiding. Data is available only
through a predefined interface ( function set and show). Otherwise it is inaccessible; in other
words, invisible.
– Polymorphism: we can design new classes based on a single class. Imagine we have defined a
general class animal. Now we may develop new classes like tiger, wolf, hare and so on. These
classes have many things common with animal class. They have few properties which are
different. They will have many functions with same name like RUN. Though speed of these
animals is quite different, they will share a similar interface. This is the principle of
polymorphism. When same function applied on different object will give you different
results.
– Message passing: when objects wants to communicate with each other, they do so in terms
of messages. Message is information sent by one object to other.
inheritance
class person /*Parent class*/ {
private:
char fname[100],lname[100],gender[10];
protected: int age;
public: void input_person();
void display_person();
};
– Code sharing: reusability refers to using our own code, while code sharing
refers to code written by our friends
– Class DATE
{ private:
int date, month, year;
public:
set_date();
show_date();
}
• In this the class name is DATA.
• It has three data members vz date, month and
year which are private.
• It also has two public methods viz set_date()
and show_date()
• If you notice one of the three data members
of class DATE has name date. In C++ DATE and
date are distinct identifiers.
• Class name uses all capital letters
• Encapsulation
– The process of holding together data members
and method members in a single variable type
called class is called encapsulation.
– Encapsulation not only makes a class almost a self
contained entity, but also protects the data from
inadvertent processing by other functions,
independently written or methods of other
classes.
– Look at private qualifier of the data members,
making them private ensures their safety. The
private data can be processed by member
methods only.
• Declaration of objects
– Class represents a general template that shows how a
students data is stored and processed.
– How do we process each students data? Then why
objects are declared?
• If there are three students , three objects say S1, S2,S3 are
declared.
– For example,
STUDENT S1,S2,S3;
– S1,S2,S3 are objects of the class STUDENT.
– There fore an object is an instance of a class.
– In a programmer’s words an object is a variable of user defined
data type
– It means S1,S2,S3 are variables of class STUDENT.
– The word variable implies that contents of these objects can vary
(just like inbuilt type int)
• Qualifying data members :
– Class is an aggregate type like struct.
– A member of the aggregate has to be qualified to avoid ambiguity.
• If there are two cricket teams, say india and newzealand, and we havt to
talk about the captains, we say indian captain or newzealand captain.
Saying only captain creates ambiguity-which captain?
• Similarly, if we simply refer to roll no., there is ambugity, whose roll no?
We have to qulify thus
– S1.roll_number, it refers to S1s roll no
– S2.roll_number, it refers to S2s roll no and so on.
• Let us take another example of object declaration
– DATE d1,d2,d3;
– d1, d2, d3 are objects which hold dates like 19/12/2003, 15/08/1947 and so
on
– It is possible to use pointers for specifying objects. We have to
declare as show below;
• STUDENT * ptr1;
– Here we declare a pointer to a class STUDENT. We can initialize this
pointer with address of s1, that is
– Ptr1 = &s1;
– Then qualify members using pointer arrow mechanism , for example
– Ptr1 ->roll_number instead of s1. roll_number
• Data hiding and encapsulation
– Principle of Data hiding
• Private and public members
– The trouble with the last example was that the data members were
private. Let us declare them as public and see the result
• The only change we made is to add the keyword public. It allowed
us to modify and see/print the variable. ‘private’ and ‘public’ are
access specifiers for the members of a class.
• They specify whether the members can be accessed freely or not.
• A class with only public data members will work similar to structure.
• What is advantage of class? In this example class was limited only to
public and private data members.
• Unlike structures in c, a class also contains methods that operate on
data members and process them.
• Now the scope of the data members and the functions can be so
defined such that data members of a class can normally be
processed only by these function members and not by methods of
other classes or independently written function.