AFRICA CODER
Ir al canal en Telegram
We are helping you to learn programming We provide Short Notes, video, and also pdf of Programming Languages Join Our Group And Subscribe For more info. We grow up together!! for any suggestion contact me👉 @bluefannn
Mostrar másEl país no está especificadoTecnologías y Aplicaciones67 893
770
Suscriptores
Sin datos24 horas
Sin datos7 días
Sin datos30 días
Archivo de publicaciones
770
Do-while Loop Statement
The do-while loop is similar to the while loop, except that the test condition is performed
at the end of the loop instead of at the beginning. The do—while loop executes atleast once
without checking the condition.
It begins with the keyword do, followed by the statements that making up the body of the
loop. Finally, the keyword while and the test expression completes the do-while loop. When
the loop condition becomes false, the loop is terminated and execution continues with the
statement immediately following the loop.
The syntax of the do-while loop is
do
while ();
The following program explains the do--while statement.
public class DoWhileLoopDemo {
public static void main(String[] args) {
int count = 1;
System.out.println(“Printing Numbers from 1 to 10”);
do {
System.out.println(count++);
} while (count <= 10);
}
}
Thanks 🙏😊
770
Objects And Classes
Object
Objects have states and behaviors. Example: A dog has states - color, name, breed as well
as behaviors – wagging the tail, barking, eating. An object is an instance of a class.
Class
A class can be defined as a template/blueprint that describes the behavior/state that the
object of its type support.
Objects in Java
If we consider the real-world, we can find many objects around us, cars, dogs, humans,
etc. All these objects have a state and a behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking,
wagging the tail, running.
If we compare the software object with a real-world object, they have very similar characteristics.
Software objects also have a state and a behavior. A software object’s state is stored in
fields and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the
object-to-object communication is done via methods.
770
Static Keyword
The static keyword indicates that the member belongs to the class instead of a specific
instance. It is used to create class variable and mainly used for memory management. The
static keyword can be used with:
• Variable (static variable or class variable)
• Method (static method or class method)
• Block (static block)
• Nested class (static class)
• import (static import)
Static variable
Variable declared with keyword static is a static variable. It is a class level variable commonly shared by all objects of the class.
• Memory allocation for such variables only happens once when the class is loaded in
the memory.
• scope of the static variable is class scope ( accessible only inside the class)
• lifetime is global ( memory is assigned till the class is removed by JVM).
• Automatically initialized to 0.
• It is accessible using ClassName.variablename
• Static variables can be accessed directly in static and non-static methods
770
The Task of a Constructor
Traditional programming languages only allocate memory for a variable being defined.
The programmer must ensure that the variable is initialized with suitable values.
An object of the class Account, as described in the previous chapter, does not possess
any valid values until the method init() is called. Non-initialized objects can lead to
serious runtime errors in your programs.
To avoid errors of this type, C++ performs implicit initialization when an object is
defined. This ensures that objects will always have valid data to work on. Initialization is
performed by special methods known as constructors.
Declaration
Constructors can be identified by their names. In contrast to other member functions,
the following applies:
■ the name of the constructor is also the class name
■ a constructor does not possess a return type—not even void.
Constructors are normally declared in the public section of a class. This allows you to
create objects wherever the class definition is available.
Constructors can be overloaded, just like other functions. Constructors belonging to a
class must be distinguishable by their signature (that is, the number, order, and type of
parameters). This allows for different methods of object initialization. The example
opposite shows an addition to the Account class. The class now has two constructors.
Definition
Since a constructor has the same name as its class, the definition of a constructor always
begins with
Class_name::Class_name
In the definition itself, the arguments passed can be checked for validity before they are
copied to the corresponding data members. If the number of arguments is smaller than
the number of data members, the remaining members can be initialized using default values.
Constructors can also perform more complex initialization tasks, such as opening files,
allocating memory, and configuring interfaces.
@Ethiopian programmer
770
1 👉Data Abstraction
Humans use abstraction in order to manage complex situations. Objects and processes are
reduced to basics and referred to in generic terms. Classes allow more direct use of the
results of this type of abstraction in software development.
The first step towards solving a problem is analysis. In object-oriented programming,
analysis comprises identifying and describing objects and recognizing their mutual relationships. Object descriptions are the building blocks of classes.
In C++, a class is a user-defined type. It contains data members, which describe the
properties of the class, and member functions, or methods, which describe the capacities of
the objects. Classes are simply patterns used to instantiate, or create, objects of the class
type. In other words, an object is a variable of a given class.
2 👉 Data Encapsulation
When you define a class, you also specify the private members, that is, the members that
are not available for external access, and the public members of that class. An application program accesses objects by using the public methods of the class and thus activating its capacities.
Access to object data is rarely direct, that is, object data is normally declared as private and then read or modified by methods with public declarations to ensure correct
access to the data.
One important aspect of this technique is the fact that application programs need not
be aware of the internal structure of the data. If needed, the internal structure of the program data can even be modified. Provided that the interfaces of the public methods
remain unchanged, changes like these will not affect the application program. This
allows you to enhance an application by programming an improved class version without
changing a single byte of the application.
An object is thus seen to encapsulate its private structure, protecting itself from external influences and managing itself by its own methods. This describes the concept of data
encapsulation concisely
@Ethiopian programmer 😊
770
Margaret Hamilton, née Margaret Heafield, (born August 17, 1936, Paoli, Indiana, U.S.), American computer scientist who was one of the first computer software programmers; she created the term software engineer to describe her work.
THAT'S WHY PROGRAMMING IS DIFFICULT TO UNDERSTAND🤣🤣🤣
HAVE A NICE DREAM 🙏
770
Passing by Reference
A pass by reference can be programmed using references or pointers as function parameters. It is syntactically simpler to use references, although not always permissible.
A parameter of a reference type is an alias for an argument. When a function is called,
a reference parameter is initialized with the object supplied as an argument. The function
can thus directly manipulate the argument passed to it.
Example: void test( int& a) { ++a; }
Based on this definition, the statement
test( var); // For an int variable var
increments the variable var. Within the function, any access to the reference a automatically accesses the supplied variable, var.
If an object is passed as an argument when passing by reference, the object is not
copied. Instead, the address of the object is passed to the function internally, allowing
the function to access the object with which it was called.
Comparison to Passing by Value
In contrast to a normal pass by value an expression, such as a+b, cannot be used as an
argument. The argument must have an address in memory and be of the correct type.
Using references as parameters offers the following benefits:
■ arguments are not copied. In contrast to passing by value, the run time of a program should improve, especially if the arguments occupy large amounts of memory
■ a function can use the reference parameter to return multiple values to the calling
function. Passing by value allows only one result as a return value, unless you
resort to using global variables.
If you need to read arguments, but not copy them, you can define a read-only reference
as a parameter.
Example: void display( const string& str);
The function display() contains a string as an argument. However, it does not generate a new string to which the argument string is copied. Instead, str is simply a reference to the argument. The caller can rest assured that the argument is not modified
within the function, as str is declared as a const.
