Object Modeling and C++ Programming For Bca 2nd Semester PDF
Object Modeling and C++ Programming For Bca 2nd Semester PDF
BCA 201
Unit-1
Object modeling
Object and Classes-
Class- A template or blueprint that defines the characteristics of an
object and describes how the object should look and behave.
Object- Object is an instance of a class. Combining both data and
member functions. Objects are the basic run-time entities in an object-
oriented system.
Abstract Class-
An abstract class is a class that is designed to be specifically
used as a baseclass. An abstract class contains at least one pure
virtual function. You declare a pure virtual function by using a
pure specifier ( = 0 ) in the declaration of a virtual member
function in the class declaration.
Multiple Inheritance-
Multiple inheritance is a feature of some object-oriented
computer programming languages in which an object or class
can inheritcharacteristics and features from more than one
parent object or parent class.
Meta Data-
Metadata is data that describes other data. Meta is a prefix that
in most information technology usages means "an underlying
definition or description."
Metadata is data that describes other data. Meta is a prefix that
in most information technology usages means "an underlying
definition or description."
Candidate Key-
A candidate key is a column, or set of columns, in a table that
can uniquely identify any database record without referring to
any other data. Each table may have one or more candidate
keys, but one candidate key is unique, and it is called
the primary key.
Constraints-
A constraint is a packageable element which represents some
condition, restriction or assertion related to some element
(that owns the constraint) or several elements. Constraint is
usually specified by a Boolean expression which must evaluate
to a true or false.
Unit-2
Dynamic Modeling
Dynamic Modeling is used to represent the behavior of the
static constituents of a software , here static constituents
includes, classes , objects, their relationships and interfaces
Dynamic Modeling also used to represents the interaction,
workflow, and different states of the static constituents in a
software.
Event And States-
Events-
Events are some occurrences that can trigger state
transition of an object or a group of objects. Events have
a location in time and space but do not have a time
period associated with it.
Examples of events are mouse click, key press, an
interrupt, stack overflow, etc.
States-
State transition diagrams or state machines describe the
dynamic behavior of a single object. It illustrates the
sequences of states that an object goes through in its
lifetime, the transitions of the states, the events and
conditions causing the transition and the responses due
to the events.
Operations-
Activity is an operation upon the states of an object that
requires some time period. They are the ongoing
executions within a system that can be interrupted.
Activities are shown in activity diagrams that portray
the flow from one activity to another.
Functional Modeling
Data Flow Diagram-
A Data Flow Diagram (DFD) is traditional visual representation
of the information flows within a system.
It shows how information enters and leaves the system, what
changes the information and where information is stored.
It may be used as a communications tool between a systems
analyst and any person who plays a part in the system that acts
as the starting point for redesigning a system.
Specifying Operations-
Functional Modelling gives the process perspective of the
object-oriented analysis model and an overview of what the
system is supposed to do.
It defines the function of the internal processes in the system
with the aid of Data Flow Diagrams (DFDs).
It depicts the functional derivation of the data values without
indicating how they are derived when they are computed, or
why they need to be computed.
Constraints-
A constraint is a packageable element which represents some
condition, restriction or assertion related to some element
(that owns the constraint) or several elements. Constraint is
usually specified by a Boolean expression which must evaluate
to a true or false.
Basic concepts-
=> there are various type of basic concept such as-
1) Class
2) Object
3) Data Abstaraction
4) Polymorhpihsm
5) Encapsulation
6) Inheritance
7) Dynamic Binding
8) Message passing
Basics of C++ -
In this section we will cover the basics of C++, it will include the
syntax, variable, operators, loop types, pointers, references and
information about other requirements of a C++ program. You
will come across lot of terms that you have already studied in C
language.
Structure-
Structure is a collection of variables of different data types
under a single name. It is similar to a class in that, both holds a
collecion of data of different data types.
Tokens–
A token is the smallest element of a C++ program that is
meaningful to the compiler. The C++ parser recognizes these
kinds of tokens: identifiers, keywords, literals, operators,
punctuators, and other separators. A stream of
these tokens makes up a translation unit.
Data Types–
In computer science and computer programming, a data
type or simply type is a classification of data which tells the
compiler or interpreter how the programmer intends to use
the data.
Most programming languages support various types of data-
for example: real, integer or Boolean.
Dynamic Initialization–
According to the C/C++ standards global variables should
be initialized before entering main(). In the above program,
variable 'i' should be initialized by return value of function
alpha(). Since the return value is not known until the program
is actually executed, this is called dynamic initialization of
variable.
Referance Variables–
A reference variable is an alias, that is, another name for an
already existing variable. Once a reference is initialized with a
variable, either the variable name or the reference name may
be used to refer to the variable.
#include <iostream>
int main () {
// declare simple variables
int i;
double d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
Operators–
An operator is a character that represents an action, as for
example x is an arithmetic operator that represents
multiplication. In computer programs, one of the most familiar
sets ofoperators, the Boolean operators, is used to work with
true/false values.
Manipulators–
Manipulators are functions specifically designed to be used in
conjunction with the insertion (<<) and extraction (>>)
operators on stream objects, for example: cout << boolalpha;
... Manipulators are used to change formatting parameters on
streams and to insert or extract certain special characters.
Control Structure–
A program is usually not limited to alinear sequence of
instructions. During its process it may bifurcate, repeat code or
take decisions. For that purpose, C++ provides control
structures that serve to specify what has to be done by our
program, when and under which circumstances.
Functions In C++
Introduction-
A function is a group of statements that together perform a
task. Every C++ program has at least onefunction, which is
main(), and all the most trivial programs can define
additional functions. You can divide up your code into
separate functions.
Main()function-
the role of main() is to indicate to the compiler to convert
source code from { open curly bracket to } close curly bracket.
Prototyping-
The Prototyping Model is a systems development method
(SDM) in which a prototype (an early approximation of a final
system or product) is built, tested, and then reworked as
necessary until an acceptable prototype is finally achieved
from which the complete system or product can now be
developed.
Inline function-
The inline functions are a C++ enhancement feature to increase
the execution time of a program.
Functions can be instructed to compiler to make them inline so
that compiler can replace those function definition wherever
those are being called.
NOTE- This is just a suggestion to compiler to make the
function inline, if function is big (in term of executable
instruction etc) then, compiler can ignore the “inline” request
and treat the function as normal function.
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
} //Output: The cube of 3 is: 27
Default arguments-
A default argument is a value provided in function declaration
that is automatically assigned by the compiler if caller of the
function doesn't provide a value for the argument with default
value. Following is a simple C++ example to demonstrate use of
default arguments.
function overloading-
C++ allows specification of more than one function of the same
name in the same scope. These are called overloaded functions
and are described in detail in Overloading. Overloaded
functions enable programmers to supply different semantics
for a function, depending on the types and number of
arguments.
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char* c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
friend functions-
C++ Friend Functions. A friend function of a class is defined
outside that class' scope but it has the right to access all private
and protected members of the class. Even though the
prototypes for friend functions appear in the class definition,
friends are not member functions.
#include <iostream>
class A {
private:
int a;
public:
A() { a=0; }
friend class B; // Friend Class
};
class B {
private:
int b;
public:
void showA(A& x) {
// Since B is friend of A, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
};
int main() {
A a;
B b;
b.showA(a);
return 0;
}
};
void main ( )
{
clrscr ( );
student st;
// st.read ( ); // not accessible
st.show ( );
getch();
}
{
int mount;
auto int month;
}
// Function declaration
void func(void);
main() {
while(count--) {
func();
}
return 0;
}
// Function definition
void func( void ) {
static int i = 5; // local static variable
i++;
std::cout << "i is " << i ;
std::cout << " and count is " << count << std::endl;
}
Introduction-
constructor is a very special member function whose name is
same as class name. consturtor is self executable when object
is created. consturctor is always declare inside a public mode.
the role of the consturcotr is inilized the value.
constructor is not required any kind of return type not even
void
Type of constuctor-
constructe can be categoriese in 4 type-
1) default
2) non paramertized
3) paramerized constructor
4) copy cocnstrucor
Parameterized constructors-
It may be necessary to initialize the various data elements of
different objects with different values when they are created.
This is achieved by passing arguments to the constructor
function when the objects are created. The constructors that
can take arguments are called parameterized constructors.
Multiple constructors in a class-
class number
{
int a,b;
public :
number ()
{
a=0;
b=0;
}
void show()
{
cout<< a<<b;
};
or
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int main()
{
Point p1(10, 15); // Normal constructor is called
here
Point p2 = p1; // Copy constructor is called here
return 0;
}
Destructors-
A destructor is a special member function of a class that is
executed whenever an object of it's class goes out of scope or
whenever the delete expression is applied to a pointer to the
object of that class.
A destructor will have exact same name as the class prefixed
with a tilde (~) and it can neither return a value nor can it take
any parameters.
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
Operator Overloading
Introduction-
Operator overloading allows you to redefine the way operator
works for user-defined types only (objects, structures). It
cannot be used for built-in types (int, float, char etc.). Two
operators = and & are already overloaded by default in C++.
For example: To copy objects of same class, you can directly
use = operator.
Method of overloading-
Function Overloading
If any class have multiple functions with same names but
different parameters then they are said to be overloaded.
Function overloading allows you to use the same name for
different functions, to perform, either same or different
functions in the same class.
Function overloading is usually used to enhance the readability
of the program. If you have to perform one single operation but
with different number or types of arguments, then you can
simply overload the function.
int sum (int x, int y)
{
cout << x+y;
}
Inc(int C) {
// Constructor with Argument
count = C ;
}
Inc operator ++ () {
// Operator Function Definition
return Inc(++count);
}
Inc operator -- () {
// Operator Function Definition
return Inc(--count);
}
void display(void) {
cout << count << endl ;
}
};
void main(void) {
Inc a, b(4), c, d, e(1), f(4);
++a;
b++;
--e;
f--;
int main ()
{
string First = "This is First String and ";
string Second = "This is Second String.";
string Third = First + Second;
return 0;
}
Virtual functions-
A virtual function a member function which is declared within
base class and is re-defined (Overriden) by derived class.When
you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for
that object and execute the derived class’s version of the
function.
Virtual functions ensure that the correct function is called for
an object, regardless of the type of reference (or pointer) used
for function call.
They are mainly used to achieve Runtime polymorphism.
Functions are declared with a virtual keyword in base class.
The resolving of function call is done at Run-time.
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
The pointers pointing to objects are referred to as Object
Pointers.
C++ Declaration and Use of Object Pointers
Just like other pointers, the object pointers are declared by
placing in front of a object pointer's name. It takes the following
general form :
class-name ∗ object-pointer ;
where class-name is the name of an already defined class and
object-pointer is the pointer to an object of this class type. For
example, to declare optr as an object pointer of Sample class
type, we shall write.
Sample ∗optr ;
where Sample is already defined class. When accessing
members of a class using an object pointer, the arrow
operator (->) is used instead of dot operator.
#include<iostream.h>
#include<conio.h>
class Time
{
short int hh, mm, ss;
public:
Time()
{
hh = mm = ss = 0;
}
void getdata(int i, int j, int k)
{
hh = i;
mm = j;
ss = k;
}
void prndata(void)
{
cout<<"\nTime is
"<<hh<<":"<<mm<<":"<<ss<<"\n";
}
};
void main()
{
clrscr();
Time T1, *tptr;
cout<<"Initializing data members using the object, with
values 12, 22, 11\n";
T1.getdata(12,22,11);
cout<<"Printing members using the object ";
T1.prndata();
tptr = &T1;
cout<<"Printing members using the object pointer ";
tptr->prndata();
cout<<"\nInitializing data members using the object
pointer, with values 15, 10, 16\n";
tptr->getdata(15, 10, 16);
cout<<"printing members using the object ";
T1.prndata();
cout<<"Printing members using the object pointer ";
tptr->prndata();
getch();
}