This document provides an introduction to C/C++ programming languages. It discusses the history of computing from ancient mechanical devices to modern computers. Some key figures discussed include Charles Babbage, Alan Turing, John Backus who invented FORTRAN, and Bjarne Stroustrup who created C++. The document outlines the topics to be covered which include functions, dynamic memory, structures, classes, file I/O and code examples. It provides an overview of flowcharts and complexity classes in computation. The first lab asks students to learn how to create websites and share results electronically.
Cracking the Enigma Machine - Rejewski, Turing and the Math that saved the worldBradYoung
This presentation demonstrates the historical and mathematical background to the brilliant work done by Polish and British cryptology experts before and during World War II.
The solutions provided by Marian Rejewski, Alan Turing and their co-workers had a major impact on the outcome of the war.
The document lists various events and inventions related to the history of computing and technology. It includes the development of switchboards in 1877, the digital clock invented in 1972 by Hamilton, MD5 hashing developed by Ronald Rivest, Amazon's Dynamo system, and other topics like sundials, binoculars, mailboxes, graphs, pressure measurement, SMS, feedback amplifiers, and monitoring systems. Many are attributed to specific people and dates.
This document provides a 3-page summary of the history of computation from antiquity to modern times. It discusses early mechanical computers like the ancient Greek Antikythera mechanism and abacuses. It then covers the development of programming languages and computers through figures like Babbage, Turing, and von Neumann. It describes early digital computers in the 1940s-50s and the development of semiconductors, microprocessors, and personal computers in subsequent decades.
The document provides an introduction to computer science for grade 11 students. It discusses the definition of a computer, the input-process-output cycle, characteristics of computers such as speed, accuracy, storage capacity. It also covers the history of computers from the mechanical era to modern electronic computers. The exam format for grade 11 is outlined, with full marks of 75 and pass marks of 24.
- The document provides a brief history of computing from ancient times to modern computers. It discusses early calculating devices like the abacus and how they evolved over time.
- Important figures that advanced computing are mentioned, like Charles Babbage who designed analytical engines, Ada Lovelace who wrote about programming the analytical engine, and Herman Hollerith who developed tabulating machines.
- Milestones like the first general purpose electronic computer (ENIAC), stored program concept, and first commercial computers are summarized. The evolution of computers from mainframes to minicomputers to microprocessors is covered at a high level.
- The document provides a brief history of computing from ancient times to modern computers. It discusses early computing devices like the abacus and how they evolved over time.
- It describes pioneers in computing like Charles Babbage and Ada Lovelace and their work on early mechanical computers in the 1800s. It also covers the development of electromechanical computers in the 1900s and early electronic digital computers in the 1940s.
- The summary highlights the development of stored-program architecture and programming languages as well as the emergence of mainframe, mini, and microcomputers that drove computing innovation from the 1940s through the 1970s.
This document outlines a course on control systems taught by Qazi Ejaz Ur Rehman. It includes an introduction to the subject, an overview of topics to be covered such as signals and systems, modeling, frequency and time domains, software, and labs. The course will utilize MATLAB and LabVIEW for simulations and experiments. The document provides biographies of important figures in the history of computing and mathematics relevant to control systems.
PCCAT Keynote 1jun12: We are already designing integrated circuits with more than a Billion transistors; and despite the faltering steps of Moore's law, we will be designing more than 10 times that within the next 5yrs. How will industry ever deliver a complete design in a reasonable time-frame today? The answer is Reuse and Hierarchy, both vitally important tools, yet neither academic topics in their own right.
Cracking the Enigma Machine - Rejewski, Turing and the Math that saved the worldBradYoung
This presentation demonstrates the historical and mathematical background to the brilliant work done by Polish and British cryptology experts before and during World War II.
The solutions provided by Marian Rejewski, Alan Turing and their co-workers had a major impact on the outcome of the war.
The document lists various events and inventions related to the history of computing and technology. It includes the development of switchboards in 1877, the digital clock invented in 1972 by Hamilton, MD5 hashing developed by Ronald Rivest, Amazon's Dynamo system, and other topics like sundials, binoculars, mailboxes, graphs, pressure measurement, SMS, feedback amplifiers, and monitoring systems. Many are attributed to specific people and dates.
This document provides a 3-page summary of the history of computation from antiquity to modern times. It discusses early mechanical computers like the ancient Greek Antikythera mechanism and abacuses. It then covers the development of programming languages and computers through figures like Babbage, Turing, and von Neumann. It describes early digital computers in the 1940s-50s and the development of semiconductors, microprocessors, and personal computers in subsequent decades.
The document provides an introduction to computer science for grade 11 students. It discusses the definition of a computer, the input-process-output cycle, characteristics of computers such as speed, accuracy, storage capacity. It also covers the history of computers from the mechanical era to modern electronic computers. The exam format for grade 11 is outlined, with full marks of 75 and pass marks of 24.
- The document provides a brief history of computing from ancient times to modern computers. It discusses early calculating devices like the abacus and how they evolved over time.
- Important figures that advanced computing are mentioned, like Charles Babbage who designed analytical engines, Ada Lovelace who wrote about programming the analytical engine, and Herman Hollerith who developed tabulating machines.
- Milestones like the first general purpose electronic computer (ENIAC), stored program concept, and first commercial computers are summarized. The evolution of computers from mainframes to minicomputers to microprocessors is covered at a high level.
- The document provides a brief history of computing from ancient times to modern computers. It discusses early computing devices like the abacus and how they evolved over time.
- It describes pioneers in computing like Charles Babbage and Ada Lovelace and their work on early mechanical computers in the 1800s. It also covers the development of electromechanical computers in the 1900s and early electronic digital computers in the 1940s.
- The summary highlights the development of stored-program architecture and programming languages as well as the emergence of mainframe, mini, and microcomputers that drove computing innovation from the 1940s through the 1970s.
This document outlines a course on control systems taught by Qazi Ejaz Ur Rehman. It includes an introduction to the subject, an overview of topics to be covered such as signals and systems, modeling, frequency and time domains, software, and labs. The course will utilize MATLAB and LabVIEW for simulations and experiments. The document provides biographies of important figures in the history of computing and mathematics relevant to control systems.
PCCAT Keynote 1jun12: We are already designing integrated circuits with more than a Billion transistors; and despite the faltering steps of Moore's law, we will be designing more than 10 times that within the next 5yrs. How will industry ever deliver a complete design in a reasonable time-frame today? The answer is Reuse and Hierarchy, both vitally important tools, yet neither academic topics in their own right.
Adaptive Compilation by Jecel Mattos de Assumpção JrFAST
Adaptive compilation is a technique where a just-in-time (JIT) compiler dynamically analyzes code performance and optimizes "hot spots" at runtime. Early work in the 1980s introduced JIT compilation to improve interpreter performance. Later systems like Self used "type feedback" to infer types during execution and guide adaptive recompilation for optimization. The goal is to achieve near-native performance without ahead-of-time compilation by profiling and specializing generated code based on actual usage patterns.
The document summarizes the evolution of computers from early mechanical calculating devices like the abacus and slide rule to modern electronic computers. It describes key developments like Pascal's calculating machine (1642), Babbage's analytical engine (1833), the first programmable electronic computer ENIAC (1946), the first stored-program computer EDVAC (1949), the first microprocessor (1969), and the first personal computer Apple-1 (1977). The evolution has progressed from mechanical to electro-mechanical to electronic digital computers driven by technological advancements.
The document discusses ESL (electronic system level) design and some challenges with adopting ESL flows that use C/C++ as a design entry language. It provides examples of how C/C++ code can unintentionally result in inefficient hardware implementations if the designer does not consider the hardware implications. The document advocates that ESL adoption needs to be driven by designer needs and preferences rather than management decisions. It also argues that ESL tools need to provide predictability of results, education for designers on the hardware implications of different coding styles, and robust verification methods for ESL to be widely adopted.
The document discusses a computer organization and architecture course. It provides an overview of course topics including computing environments, combinational circuits, number systems, and base conversions. It also gives examples of Boolean algebra, truth tables, Karnaugh maps, and minimizing logic functions. Combinational circuits are represented using algebraic expressions, truth tables, and circuit diagrams. Methods for simplifying Boolean functions include algebraic manipulation, Karnaugh maps, and the Quine-McCluskey algorithm.
This document provides an overview of the history and products of HCL, an Indian information technology company. It discusses that HCL was founded in 1976 and was a pioneer in the Indian IT industry, developing India's first indigenous microcomputer in 1978. The document then outlines some key events and achievements in HCL's history from the 1970s to present day. Finally, it lists several home PC products that HCL currently offers, providing the specifications for each.
Self-Replication and the Halting ProblemHiroki Sayama
Von Neumann proposed that an artificial self-replicating system would require a universal constructor that can build copies of itself by reading its own description. This is analogous to Turing machines that can emulate any other machine given its software instructions. Turing later proved that the halting problem, determining if a program will finish running, is undecidable. His proof works by considering a machine M' that determines if another machine M halts on itself as input, leading to circular self-reference that cannot be resolved. Similarly, attempting to solve the halting problem through self-replication results in an endless circular process of self-reproduction, showing the fundamental limits of self-replication and computation.
The document provides an overview of the history and components of computers. It discusses early mechanical calculating devices and then outlines the five generations of computers, describing the technology used such as vacuum tubes, transistors, integrated circuits, and artificial intelligence. It also categorizes different types of computers such as analog, digital, hybrid, supercomputers, mainframes, desktops, laptops, tablets, and more. The document contains a detailed but concise history of the evolution of computer technology.
This document provides an introduction to HCL, an Indian information technology company founded in 1976. It discusses HCL's history and timeline, highlighting that HCL developed India's first indigenous microcomputer in 1978. It also notes some of HCL's achievements and innovations over the decades in areas like Unix operating systems and contract research and development. The document concludes by listing some current home PC products offered by HCL.
The document provides a history of computers from ancient counting devices like the abacus to modern personal computers. It describes early mechanical counting machines and computers like Pascal's calculator and Babbage's Analytical Engine. The development of vacuum tubes and transistors are highlighted as enabling the first electronic computers in the 1940s like ENIAC and UNIVAC. The invention of the integrated circuit led to the development of microprocessors and personal computers in the 1970s and 1980s by companies like Apple, IBM and Microsoft.
Vision-Based Obstacle Recognition and Autonomous Flights through Gate Structu...Florian-Michael Adolf
The challenge for unmanned aerial vehicles to sense and avoid obstacles becomes even harder if narrow passages have to be passed for which no precise a-priori position information is available. Inspired by recent UAV flight competitions, this work presents a vision-based approach to search for a narrow gate and to fly through it autonomously. The gate’s precise position has to be detected in order to avoid a collision. Using a GPS-based self localization, the camera alignment and the gate position are estimated simultaneously. The presented approach alters a set of waypoints to fly through the gate. All algorithms run onboard the vehicle such that the gate is passed autonomously. Results from flight tests are presented that underline the feasibility of the presented approach.
CrestaTV is the next step in the evolution of computing by bringing in Live Broadcast in addition to all the Music Pictures Documents and Contacts we carry with us.
The document summarizes the evolution of computing from Charles Babbage's Analytical Engine in 1833 to modern quantum computing. It outlines several important milestones, including: the development of punched cards in 1801, Alan Turing's theoretical Turing Machine in 1936, the first general-purpose electronic computer (ENIAC) in 1946, the invention of the transistor in 1947 replacing vacuum tubes, the development of programming languages like BASIC in the 1960s, the creation of ARPANET in 1969 which led to the Internet, and the World Wide Web in 1989. The document suggests quantum computing may be the next major milestone, allowing computers to examine all possible answers simultaneously.
1) Optimization-based OR seeks to find optimal solutions to problems by minimizing or maximizing objective functions.
2) An example is finding the optimal aerodynamic design of an airplane to maximize efficiency.
3) This involves defining decision variables like wing dimensions and an objective function to minimize, then finding the values of the variables that achieve the optimal value of the function.
Presentation for a lecture in the doctoral series at Stefan cel Mare University, Suceava, Romania, May 2009.
Aim was to show current generation the rich history of computer hardarware and that many of the recent innovations in CPU design have their origins in designs of teh 50s and 60s.
Cryptography involves transforming data using an algorithm and key to make it unreadable. Common techniques include encryption algorithms like the Caesar cipher which shifts letters by a set number, and the Vigenère cipher which uses a keyword for polyalphabetic substitution. Modern standards like the Data Encryption Standard and Advanced Encryption Standard use confusion and diffusion principles with substitution boxes and key lengths of 128+ bits for security. Key exchange methods like Diffie-Hellman allow secure key agreement without prior secrets.
This document provides information about the Computer Engineering and Sciences course CE1100. It outlines that the workload is 3 hours per week including homework, projects, tests, and a final test. It provides tips for learning such as reading books, typing lectures, and writing own ideas. It lists the class times as Sundays and discussion sections on Fridays. Office hours are Saturdays. Grades are based on homework, tests, projects, midterm, second exam, and the final exam. Prerequisites include CS1100. The document also provides an overview of computing history, modern computer systems, hardware and software interactions, careers in computer fields, and choosing a career.
The document discusses the history of computing and software development. It describes Charles Babbage as the inventor of the first mechanical computer in the 19th century. It then discusses Ada Lovelace, who wrote the first computer program for Babbage's Analytical Engine. Finally, it covers Alan Turing and his work in developing the Turing machine and breaking the Enigma code during World War 2, laying the foundations for modern computing.
Introduction to functional programming using Ocamlpramode_ce
The document provides an introduction to functional programming concepts using Ocaml as the teaching language. It outlines the workshop plan which will cover Ocaml syntax, important functional programming concepts, and understanding the functional paradigm through Ocaml. It then discusses Ocaml background, where Ocaml is used, the Ocaml REPL, function definition, type inference, recursion, tail calls and TCO, higher order functions, algebraic data types, pattern matching, immutability, and more.
25 лет истории C++, пролетевшей на моих глазахcorehard_by
Автор доклада познакомился с C++ в 1991-ом году, а с 1992-го года C++ является для докладчика основным языком разработки. Что происходило с языком за это время? Как и почему он стал популярным? Как начался застой в развитии C++? Как C++ потерял свою популярность? Есть ли место для C++ в современном мире? Попробуем поговорить об этом опираясь на 25-летний опыт программирования на C++.
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
Adaptive Compilation by Jecel Mattos de Assumpção JrFAST
Adaptive compilation is a technique where a just-in-time (JIT) compiler dynamically analyzes code performance and optimizes "hot spots" at runtime. Early work in the 1980s introduced JIT compilation to improve interpreter performance. Later systems like Self used "type feedback" to infer types during execution and guide adaptive recompilation for optimization. The goal is to achieve near-native performance without ahead-of-time compilation by profiling and specializing generated code based on actual usage patterns.
The document summarizes the evolution of computers from early mechanical calculating devices like the abacus and slide rule to modern electronic computers. It describes key developments like Pascal's calculating machine (1642), Babbage's analytical engine (1833), the first programmable electronic computer ENIAC (1946), the first stored-program computer EDVAC (1949), the first microprocessor (1969), and the first personal computer Apple-1 (1977). The evolution has progressed from mechanical to electro-mechanical to electronic digital computers driven by technological advancements.
The document discusses ESL (electronic system level) design and some challenges with adopting ESL flows that use C/C++ as a design entry language. It provides examples of how C/C++ code can unintentionally result in inefficient hardware implementations if the designer does not consider the hardware implications. The document advocates that ESL adoption needs to be driven by designer needs and preferences rather than management decisions. It also argues that ESL tools need to provide predictability of results, education for designers on the hardware implications of different coding styles, and robust verification methods for ESL to be widely adopted.
The document discusses a computer organization and architecture course. It provides an overview of course topics including computing environments, combinational circuits, number systems, and base conversions. It also gives examples of Boolean algebra, truth tables, Karnaugh maps, and minimizing logic functions. Combinational circuits are represented using algebraic expressions, truth tables, and circuit diagrams. Methods for simplifying Boolean functions include algebraic manipulation, Karnaugh maps, and the Quine-McCluskey algorithm.
This document provides an overview of the history and products of HCL, an Indian information technology company. It discusses that HCL was founded in 1976 and was a pioneer in the Indian IT industry, developing India's first indigenous microcomputer in 1978. The document then outlines some key events and achievements in HCL's history from the 1970s to present day. Finally, it lists several home PC products that HCL currently offers, providing the specifications for each.
Self-Replication and the Halting ProblemHiroki Sayama
Von Neumann proposed that an artificial self-replicating system would require a universal constructor that can build copies of itself by reading its own description. This is analogous to Turing machines that can emulate any other machine given its software instructions. Turing later proved that the halting problem, determining if a program will finish running, is undecidable. His proof works by considering a machine M' that determines if another machine M halts on itself as input, leading to circular self-reference that cannot be resolved. Similarly, attempting to solve the halting problem through self-replication results in an endless circular process of self-reproduction, showing the fundamental limits of self-replication and computation.
The document provides an overview of the history and components of computers. It discusses early mechanical calculating devices and then outlines the five generations of computers, describing the technology used such as vacuum tubes, transistors, integrated circuits, and artificial intelligence. It also categorizes different types of computers such as analog, digital, hybrid, supercomputers, mainframes, desktops, laptops, tablets, and more. The document contains a detailed but concise history of the evolution of computer technology.
This document provides an introduction to HCL, an Indian information technology company founded in 1976. It discusses HCL's history and timeline, highlighting that HCL developed India's first indigenous microcomputer in 1978. It also notes some of HCL's achievements and innovations over the decades in areas like Unix operating systems and contract research and development. The document concludes by listing some current home PC products offered by HCL.
The document provides a history of computers from ancient counting devices like the abacus to modern personal computers. It describes early mechanical counting machines and computers like Pascal's calculator and Babbage's Analytical Engine. The development of vacuum tubes and transistors are highlighted as enabling the first electronic computers in the 1940s like ENIAC and UNIVAC. The invention of the integrated circuit led to the development of microprocessors and personal computers in the 1970s and 1980s by companies like Apple, IBM and Microsoft.
Vision-Based Obstacle Recognition and Autonomous Flights through Gate Structu...Florian-Michael Adolf
The challenge for unmanned aerial vehicles to sense and avoid obstacles becomes even harder if narrow passages have to be passed for which no precise a-priori position information is available. Inspired by recent UAV flight competitions, this work presents a vision-based approach to search for a narrow gate and to fly through it autonomously. The gate’s precise position has to be detected in order to avoid a collision. Using a GPS-based self localization, the camera alignment and the gate position are estimated simultaneously. The presented approach alters a set of waypoints to fly through the gate. All algorithms run onboard the vehicle such that the gate is passed autonomously. Results from flight tests are presented that underline the feasibility of the presented approach.
CrestaTV is the next step in the evolution of computing by bringing in Live Broadcast in addition to all the Music Pictures Documents and Contacts we carry with us.
The document summarizes the evolution of computing from Charles Babbage's Analytical Engine in 1833 to modern quantum computing. It outlines several important milestones, including: the development of punched cards in 1801, Alan Turing's theoretical Turing Machine in 1936, the first general-purpose electronic computer (ENIAC) in 1946, the invention of the transistor in 1947 replacing vacuum tubes, the development of programming languages like BASIC in the 1960s, the creation of ARPANET in 1969 which led to the Internet, and the World Wide Web in 1989. The document suggests quantum computing may be the next major milestone, allowing computers to examine all possible answers simultaneously.
1) Optimization-based OR seeks to find optimal solutions to problems by minimizing or maximizing objective functions.
2) An example is finding the optimal aerodynamic design of an airplane to maximize efficiency.
3) This involves defining decision variables like wing dimensions and an objective function to minimize, then finding the values of the variables that achieve the optimal value of the function.
Presentation for a lecture in the doctoral series at Stefan cel Mare University, Suceava, Romania, May 2009.
Aim was to show current generation the rich history of computer hardarware and that many of the recent innovations in CPU design have their origins in designs of teh 50s and 60s.
Cryptography involves transforming data using an algorithm and key to make it unreadable. Common techniques include encryption algorithms like the Caesar cipher which shifts letters by a set number, and the Vigenère cipher which uses a keyword for polyalphabetic substitution. Modern standards like the Data Encryption Standard and Advanced Encryption Standard use confusion and diffusion principles with substitution boxes and key lengths of 128+ bits for security. Key exchange methods like Diffie-Hellman allow secure key agreement without prior secrets.
This document provides information about the Computer Engineering and Sciences course CE1100. It outlines that the workload is 3 hours per week including homework, projects, tests, and a final test. It provides tips for learning such as reading books, typing lectures, and writing own ideas. It lists the class times as Sundays and discussion sections on Fridays. Office hours are Saturdays. Grades are based on homework, tests, projects, midterm, second exam, and the final exam. Prerequisites include CS1100. The document also provides an overview of computing history, modern computer systems, hardware and software interactions, careers in computer fields, and choosing a career.
The document discusses the history of computing and software development. It describes Charles Babbage as the inventor of the first mechanical computer in the 19th century. It then discusses Ada Lovelace, who wrote the first computer program for Babbage's Analytical Engine. Finally, it covers Alan Turing and his work in developing the Turing machine and breaking the Enigma code during World War 2, laying the foundations for modern computing.
Introduction to functional programming using Ocamlpramode_ce
The document provides an introduction to functional programming concepts using Ocaml as the teaching language. It outlines the workshop plan which will cover Ocaml syntax, important functional programming concepts, and understanding the functional paradigm through Ocaml. It then discusses Ocaml background, where Ocaml is used, the Ocaml REPL, function definition, type inference, recursion, tail calls and TCO, higher order functions, algebraic data types, pattern matching, immutability, and more.
25 лет истории C++, пролетевшей на моих глазахcorehard_by
Автор доклада познакомился с C++ в 1991-ом году, а с 1992-го года C++ является для докладчика основным языком разработки. Что происходило с языком за это время? Как и почему он стал популярным? Как начался застой в развитии C++? Как C++ потерял свою популярность? Есть ли место для C++ в современном мире? Попробуем поговорить об этом опираясь на 25-летний опыт программирования на C++.
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
it describes the bony anatomy including the femoral head , acetabulum, labrum . also discusses the capsule , ligaments . muscle that act on the hip joint and the range of motion are outlined. factors affecting hip joint stability and weight transmission through the joint are summarized.
How to Setup Warehouse & Location in Odoo 17 InventoryCeline George
In this slide, we'll explore how to set up warehouses and locations in Odoo 17 Inventory. This will help us manage our stock effectively, track inventory levels, and streamline warehouse operations.
This presentation was provided by Steph Pollock of The American Psychological Association’s Journals Program, and Damita Snow, of The American Society of Civil Engineers (ASCE), for the initial session of NISO's 2024 Training Series "DEIA in the Scholarly Landscape." Session One: 'Setting Expectations: a DEIA Primer,' was held June 6, 2024.
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...PECB
Denis is a dynamic and results-driven Chief Information Officer (CIO) with a distinguished career spanning information systems analysis and technical project management. With a proven track record of spearheading the design and delivery of cutting-edge Information Management solutions, he has consistently elevated business operations, streamlined reporting functions, and maximized process efficiency.
Certified as an ISO/IEC 27001: Information Security Management Systems (ISMS) Lead Implementer, Data Protection Officer, and Cyber Risks Analyst, Denis brings a heightened focus on data security, privacy, and cyber resilience to every endeavor.
His expertise extends across a diverse spectrum of reporting, database, and web development applications, underpinned by an exceptional grasp of data storage and virtualization technologies. His proficiency in application testing, database administration, and data cleansing ensures seamless execution of complex projects.
What sets Denis apart is his comprehensive understanding of Business and Systems Analysis technologies, honed through involvement in all phases of the Software Development Lifecycle (SDLC). From meticulous requirements gathering to precise analysis, innovative design, rigorous development, thorough testing, and successful implementation, he has consistently delivered exceptional results.
Throughout his career, he has taken on multifaceted roles, from leading technical project management teams to owning solutions that drive operational excellence. His conscientious and proactive approach is unwavering, whether he is working independently or collaboratively within a team. His ability to connect with colleagues on a personal level underscores his commitment to fostering a harmonious and productive workplace environment.
Date: May 29, 2024
Tags: Information Security, ISO/IEC 27001, ISO/IEC 42001, Artificial Intelligence, GDPR
-------------------------------------------------------------------------------
Find out more about ISO training and certification services
Training: ISO/IEC 27001 Information Security Management System - EN | PECB
ISO/IEC 42001 Artificial Intelligence Management System - EN | PECB
General Data Protection Regulation (GDPR) - Training Courses - EN | PECB
Webinars: https://pecb.com/webinars
Article: https://pecb.com/article
-------------------------------------------------------------------------------
For more information about PECB:
Website: https://pecb.com/
LinkedIn: https://www.linkedin.com/company/pecb/
Facebook: https://www.facebook.com/PECBInternational/
Slideshare: http://www.slideshare.net/PECBCERTIFICATION
How to Add Chatter in the odoo 17 ERP ModuleCeline George
In Odoo, the chatter is like a chat tool that helps you work together on records. You can leave notes and track things, making it easier to talk with your team and partners. Inside chatter, all communication history, activity, and changes will be displayed.
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
How to Build a Module in Odoo 17 Using the Scaffold MethodCeline George
Odoo provides an option for creating a module by using a single line command. By using this command the user can make a whole structure of a module. It is very easy for a beginner to make a module. There is no need to make each file manually. This slide will show how to create a module using the scaffold method.
How to Build a Module in Odoo 17 Using the Scaffold Method
Programming v01
1. C/C++
A General-Purpose Programming Language
Engr. Qazi Ejaz Ur Rehman
Avionics Engineer
Graduate Teaching Assistant
Aeronautics & Astronautics Department
Institute of Space Technology
Islamabad
May 26, 2016
2. Outline
1 History
2 Labs
3 Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
4 Dynamic Memory
5 Structure
6 Classes
7 File I/O
8 Codes
3. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
3/105
Administration
Contact
E-mail: qaziejazurrehman@gmail.com 1
Office hours: After 11:00 am
1
qaziajazurrehman@gmail.com
4. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
4/105
Introduction
A Brief Introduction
The only mechanical device that existed for
numerical computation at the beginning of human
history was the abacus, invented in Sumeria circa
2500 BC
And is still widely used by merchants, traders and
clerks in Asia, Africa, and elsewhere
5. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
5/105
Introduction
Antikythera mechanism
The Antikythera mechanism, some time around 100 BC
in ancient Greece, is the first known analog computer
(mechanical calculator)
Designed to predict astronomical positions and eclipses
for calendrical and astrological purposes as well as the
Olympiads, the cycles of the ancient Olympic Games
6. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
6/105
Introduction
Badi al − Zaman Ab ¯u al − Izz Ism¯a ¯il
ibnal − Raz ¯azal − Jazar¯i
The Kurdish medieval scientist Al-Jazari built
programmable automata2 in 1206 AD.
Born: 1136 CE
Era: Islamic GOlden Age
Died: 1206 CE
2
Same Idea as in Movie Automata (2014)
7. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
7/105
Introduction
Johann Bernoulli 3
1667: Born in Switzerland, son of an apothecary (in
medical profession)
1738: His son, Daniel Bernoulli published
Bernoulli’s principle
Students include his son Daniel, EULER, L’Hopital
1748: Death
3
http://en.wikipedia.org/wiki/JohannBernoulli
8. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
8/105
Introduction
Leonhard Euler 4
1707: Born in Switzerland, son of a pastor
Among several other things, developed Euler’s identity,
ejω
= cos(ω) + jsin(ω)
Also developed marvelous polyhedral fromula, nowadays
written as v − e + f = 2.
Friend of his doctoral advisor’s son, Daniel Bernoulli,
who developed Bernoulli’s principle
1783: Death
4
http://en.wikipedia.org/wiki/LeonhardEuler
9. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
9/105
Introduction
Pierre-Simon Laplace 5
1749: Born in France, son of a laborer
1770-death: Worked on probability, celestial mechanics,
heat theory
1785: Examiner, examined and passed Napoleon in
exam
1790: Paris Academy of Sciences, worked with
Lavoisier, Coulomb
1827: Died
5
http://en.wikipedia.org/wiki/Pierre-Simon Laplace
10. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
10/105
Introduction
Joseph Fourier 6
1768: Born in France, son of a tailor
1789-1799: Promoted the French Revolution
1798: Went with Napoleon to Egypt and made governor
of Lower Egypt
1822: Showed that representing a function by a
trigonometric series greatly simplifies the study of heat
propagation
1830: Fell from stairs and died shortly afterward
6
http://en.wikipedia.org/wiki/JosephFourier
11. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
11/105
Introduction
Charles Babbage
Babbage is credited with inventing the first mechanical
computer that eventually led to more complex designs.
Born: 26 December 1791 London, England
Considered by some to be a father of the computer
Died: 18 October 1871 (aged 79) Marylebone, London,
England
12. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
12/105
Introduction
John Vincent Atanasoff (1903-1995)
Figure: Atanasoff, in the 1990s.
Built first digital computer in the 1930s.
13. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
13/105
Introduction
Howard Hathaway Aiken (1901-1980)
Built Mark I, during 1939-1944
Presented to public in 1944
Reaction was great
Although Mark I meant a great deal for the
development in computer science, it’s not
recognised greatly today.
The reason for this is the fact that Mark I (and also
Mark II) was not electronic - it was
electromagnetical
14. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
14/105
Introduction
J. Presper Eckert (1919-1995) and
Mauchly (1907-1980)
Built ENIAC (Electronic Numerical Integrator and
Computer), the first electronic general-purpose
computer during 1943-1945 at a cost of $468,000.
15. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
15/105
Introduction
Alan Mathison Turing7
Born: 23 June 1912
Turing is widely considered to be the father of
theoretical computer science and artificial
intelligence
Famous for Breaking Enigma Machine Code
Died: 7 June 1954 (aged 41)
7
The Imitation Game: A 2014 Movie biographied on turing
16. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
16/105
Turing Machine
. . . b b a a a a . . . Input/Output Tape
q0q1
q2
q3 ...
qn
Finite Control
q1
Reading and Writing Head
(moves in both directions)
17. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
17/105
History
FORTRAN
Inventor: John Backus
FORTRAN, derived from Formula Translating
System
It is a general-purpose, imperative programming
language that is especially suited to numeric
computation and scientific computing. Originally
developed by IBM
First Appeared: 1957; 59 years ago
18. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
18/105
History
C++
Inventor: Bjarne Stroustrup (at Bell Labs)
It is a general-purpose programming language. It has imperative,
object-oriented and generic programming features, while also providing
facilities for low-level memory manipulation
C++ is standardized by the International Organization for
Standardization (ISO)
First Appeared: 1983; 33 years ago
19. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
19/105
Excellence of Human
Equations: Changed The World
17 Equations That Changed The World
Pythagora.s Theorem a2
+ b2
= c2
Pythagoras,530 BC
Logarithms logxy = logx + logy John Napier, 1610
Calculus df
dt
= limh→0
f(t+h)−f(t)
h
Newton, 1668
Law of Gravity F = G m1m2
r2 Newton, 1687
Complex Identity i2
= −1 Euler, 1750
Polyhedra Formula V − E + F = 2 Euler, 1751
Normal Distribution φ(x) = 1√
2πρ
e
(x−µ)2
2ρ2
C.F. Gauss, 1810
Wave Equation ∂2
u
∂t2 = c2 ∂2
u
∂x2 J. d’Almbert,1746
Fourier Transform f(ω) =
∞
−∞
f(x)e−2πixω
dx J. Fourier, 1822
Navier-Stokes Equation ρ(∂v
∂t
+ v. v) = − p + .T + f C. Navier, G. Stokes,
1845
Maxwell’s Equations
E =
ρ
0
.H = 0
× E = − 1
c
∂H
∂t
× H = 1
c
∂E
∂t
J.C. Maxwell, 1865
Second Law of Ther-
mosynamics
dS ≥ 0 L. Boltzmann, 1874
Relativity E = mc2
Einstein, 1905
Schrodinger’s Equation i ∂
∂t
= H E. Schrodinger, 1927
Information Theory H = − p(x)logp(x) C. Shannon, 1949
Chaos Theory xt+1 = kxt (1 − xt ) Robert May,1975
Black-Scholes
Equation 1
2
σ2
S2 ∂2
V
∂S2 + rS ∂V
∂S
+ ∂V
∂t
− rV = 0 F. Black, M. Scholes,
1990
20. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Introduction
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
20/105
Introduction
Programming Accessories
Whatever the approach to development may be, the final
program must satisfy some fundamental properties. The
following properties are among the most important
S Reliability
S Robustness
S Usability
S Portability
S Maintainability
S Efficiency/performance
21. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
flowchart
Lab Architecture
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
21/105
Flow Chart
Start
Input
Process 1
Decision 1
Process 2a
Process 2b
Output
Stop
yes
no
22. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
flowchart
Lab Architecture
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
22/105
Flow Chart
initialize
model
expert system
identify
candidate
models
evaluate
candidate
models
update
model
is best
candidate
better?
stop
yes
no
23. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
flowchart
Lab Architecture
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
23/105
Computational Complexity Classes
LOG
Time
LOG
Space
PTIME
N
PTIM
E
NPC
co-N
PTIM
E
PSPACE
EXPTIME
EXPSPACE
.
.
.
ELEMENTARY
.
.
.
2EXPTIME
MATLAB
24. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
flowchart
Lab Architecture
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
24/105
Lab1
Learn to Record and Share Your Results Electronically
Learn how to make a website and put your results
on it
Website files must not be path dependent, i.e, if I
copy them to any location such as a USB, or
different directory, the website must still work
The main file of the website must be index.html
Many tools are available, but a good cross-platform
open source software is kompozer available from
http://www.kompozer.net/
25. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
flowchart
Lab Architecture
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
25/105
Lab1
Learn to Extend Existing Work in a Controls Topic
Make groups, pick a research topic, create a website
with following headings:
1 Introduction
2 Technical Background
3 Expected Experiments
4 Expected Results
5 Expected Conclusions
Present your website. Every group member will be
quizzed randomly. Your final work will count towards
your lab exam.
26. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
26/105
C++
Functions
£ Functions allow to structure programs in segments
of code to perform individual tasks.
£ A function is a group of statements that is given a
name, and which can be called from some point of
the program
£ type name (parameter1, parameter2, ...) { statements }
Where:
- type is the type of the value returned by the
function.
- name is the identifier by which the function can be
called.
- parameters each parameter consists of a type
followed by an identifier, with each parameter being
separated from the next by a comma.
- statements is the function’s body. And specify what
the function actually does.
27. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
27/105
Functions
// void function example
#include iostream
using namespace std;
void printmessage ()
{
cout I’m a function!;
}
int main ()
{
printmessage ();
}
28. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
28/105
Functions
// function example
#include iostream
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout The result is z;
}
29. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
29/105
C++
Functions
Arguments passed by value and by reference
w In the functions seen earlier, arguments have
always been passed by value.
w This means what is passed to the function are the
values of these arguments on the moment of the
call
* And are copied into the variables represented by
the function parameters
w In certain cases, though, it may be useful to access
an external variable from within a function.
30. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
30/105
Functions
Function with no type
// passing parameters by reference
#include iostream
using namespace std;
void duplicate (int a, int b, int c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout x= x , y= y , z=
z;
return 0;
}
31. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
31/105
C++
Functions
Efficiency considerations and const references
Call by value
* Values passed as aurgument are copied
This is a relatively inexpensive operation for
fundamental types such as int
An overhead occur if data is compound type
string concatenate (string a, string b)
{
return a+b;
}
32. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
32/105
C++
Functions
Efficiency considerations and const references
Call by reference
* Overheading can be avoided
The function operates directly on the strings passed
as arguments
The functions with reference parameters are
generally perceived as functions that modify the
arguments passed
string concatenate (string a, string b)
{
return a+b;
}
Defining arguments as constant can be used to prevent
modifying the referenced arguments for function
string concatenate (const string a, const string
b)
{
return a+b;
}
Note: using const will call this function by value in more
efficient way
33. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
32/105
C++
Functions
Efficiency considerations and const references
Call by reference
* Overheading can be avoided
The function operates directly on the strings passed
as arguments
The functions with reference parameters are
generally perceived as functions that modify the
arguments passed
string concatenate (string a, string b)
{
return a+b;
}
Defining arguments as constant can be used to prevent
modifying the referenced arguments for function
string concatenate (const string a, const string
b)
{
return a+b;
}
Note: using const will call this function by value in more
efficient way
34. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
32/105
C++
Functions
Efficiency considerations and const references
Call by reference
* Overheading can be avoided
The function operates directly on the strings passed
as arguments
The functions with reference parameters are
generally perceived as functions that modify the
arguments passed
string concatenate (string a, string b)
{
return a+b;
}
Defining arguments as constant can be used to prevent
modifying the referenced arguments for function
string concatenate (const string a, const string
b)
{
return a+b;
}
Note: using const will call this function by value in more
efficient way
35. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
33/105
C++
Inline Function
An optimized technique used by compiler to reduce
the execution time
Compiler replaces the definition of inline functions
at compile time instead of referring function
definition at runtime
For big functions (in term of executable instruction),
Compiler treat them as normal function
36. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
33/105
C++
Inline Function
An optimized technique used by compiler to reduce
the execution time
Compiler replaces the definition of inline functions
at compile time instead of referring function
definition at runtime
For big functions (in term of executable instruction),
Compiler treat them as normal function
37. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
33/105
C++
Inline Function
An optimized technique used by compiler to reduce
the execution time
Compiler replaces the definition of inline functions
at compile time instead of referring function
definition at runtime
For big functions (in term of executable instruction),
Compiler treat them as normal function
38. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
34/105
C++
Inline Function
#include iostream
using namespace std;
inline int sqr(int x)
{
int y;
y = x * x;
return y;
}
int main()
{
int a =3, b;
b = sqr(a);
cout b;
return 0;
}
39. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
35/105
C++
Inline Function
#include iostream
using namespace std;
inline int Max(int x, int y)
{
return (x y)? x : y;
}
// Main function for the program
int main( )
{
cout Max (20,10): Max(20,10) endl;
cout Max (0,200): Max(0,200) endl;
cout Max (100,1010): Max(100,1010)
endl;
return 0;
}
40. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
36/105
C++
Overloaded Function
In C++, two different functions can have the same
name
But there arguments type should be different
Compiler determines by the type of argument being
passed
41. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
36/105
C++
Overloaded Function
In C++, two different functions can have the same
name
But there arguments type should be different
Compiler determines by the type of argument being
passed
42. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
36/105
C++
Overloaded Function
In C++, two different functions can have the same
name
But there arguments type should be different
Compiler determines by the type of argument being
passed
43. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
37/105
C++
Overloaded Function
// overloading functions
#include iostream
using namespace std;
int operate (int a, int b)
{
return (a*b);
}
double operate (double a, double b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
double n=5.0,m=2.0;
cout operate (x,y) ’n’;
cout operate (n,m) ’n’;
return 0;
}
44. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
38/105
C++
Overloaded Function
// overloaded functions
#include iostream
using namespace std;
int sum (int a, int b)
{
return a+b;
}
double sum (double a, double b)
{
return a+b;
}
int main ()
{
cout sum (10,20) ’n’;
cout sum (1.0,1.5) ’n’;
return 0;
}
45. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
39/105
C++
Templates
The function could be overloaded for a lot of types
(e.g previous slide)
And it could make sense for all of them to have the
same body
C++ has the ability to define functions with generic
types, known as function templates
template class SomeType
SomeType sum (SomeType a, SomeType b)
{
return a+b;
}
46. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
39/105
C++
Templates
The function could be overloaded for a lot of types
(e.g previous slide)
And it could make sense for all of them to have the
same body
C++ has the ability to define functions with generic
types, known as function templates
template class SomeType
SomeType sum (SomeType a, SomeType b)
{
return a+b;
}
47. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
39/105
C++
Templates
The function could be overloaded for a lot of types
(e.g previous slide)
And it could make sense for all of them to have the
same body
C++ has the ability to define functions with generic
types, known as function templates
template class SomeType
SomeType sum (SomeType a, SomeType b)
{
return a+b;
}
48. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
39/105
C++
Templates
The function could be overloaded for a lot of types
(e.g previous slide)
And it could make sense for all of them to have the
same body
C++ has the ability to define functions with generic
types, known as function templates
template class SomeType
SomeType sum (SomeType a, SomeType b)
{
return a+b;
}
49. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
39/105
C++
Templates
The function could be overloaded for a lot of types
(e.g previous slide)
And it could make sense for all of them to have the
same body
C++ has the ability to define functions with generic
types, known as function templates
template class SomeType
SomeType sum (SomeType a, SomeType b)
{
return a+b;
}
50. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
40/105
C++
Template Function
// function template
#include iostream
using namespace std;
template class T
T sum (T a, T b)
{
T result;
result = a + b;
return result;
}
int main () {
int i=5, j=6, k;
double f=2.0, g=0.5, h;
k=sumint(i,j);
h=sumdouble(f,g);
cout k ’n’;
cout h ’n’;
return 0;
}
51. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
41/105
C++
Template Function
// function templates
#include iostream
using namespace std;
template class T, class U
bool are_equal (T a, U b)
{
return (a==b);
}
int main ()
{
if (are_equal(10,10.0))
cout x and y are equaln;
else
cout x and y are not equaln;
return 0;
}
52. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
42/105
C++
Non-type template arguments
// template arguments
#include iostream
using namespace std;
template class T, int N
T fixed_multiply (T val)
{
return val * N;
}
int main() {
std::cout fixed_multiplyint,2(10) ’n’;
std::cout fixed_multiplyint,3(10) ’n’;
}
53. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
43/105
C++
Scope (Local global) of variables
The point in the program where variable declaration
happens influences its visibility:
An entity declared outside any block has global
scope
While an entity declared within a block (function or a
selective statement), has block/local scope
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from
this function
}
54. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
43/105
C++
Scope (Local global) of variables
The point in the program where variable declaration
happens influences its visibility:
An entity declared outside any block has global
scope
While an entity declared within a block (function or a
selective statement), has block/local scope
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from
this function
}
55. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
43/105
C++
Scope (Local global) of variables
The point in the program where variable declaration
happens influences its visibility:
An entity declared outside any block has global
scope
While an entity declared within a block (function or a
selective statement), has block/local scope
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from
this function
}
56. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
43/105
C++
Scope (Local global) of variables
The point in the program where variable declaration
happens influences its visibility:
An entity declared outside any block has global
scope
While an entity declared within a block (function or a
selective statement), has block/local scope
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from
this function
}
57. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
43/105
C++
Scope (Local global) of variables
The point in the program where variable declaration
happens influences its visibility:
An entity declared outside any block has global
scope
While an entity declared within a block (function or a
selective statement), has block/local scope
int foo; // global variable
int some_function ()
{
int bar; // local variable
bar = 0;
}
int other_function ()
{
foo = 1; // ok: foo is a global variable
bar = 2; // wrong: bar is not visible from
this function
}
58. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
44/105
C++
Namespaces
Non-local names bring more possibilities for name
collision
Namespaces allow us to group named entities that
otherwise would have global scope into narrower
scopes,
This allows organizing the elements of programs
into different logical scopes referred to by names
namespace identifier
{
named_entities
}
59. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
44/105
C++
Namespaces
Non-local names bring more possibilities for name
collision
Namespaces allow us to group named entities that
otherwise would have global scope into narrower
scopes,
This allows organizing the elements of programs
into different logical scopes referred to by names
namespace identifier
{
named_entities
}
60. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
44/105
C++
Namespaces
Non-local names bring more possibilities for name
collision
Namespaces allow us to group named entities that
otherwise would have global scope into narrower
scopes,
This allows organizing the elements of programs
into different logical scopes referred to by names
namespace identifier
{
named_entities
}
61. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
44/105
C++
Namespaces
Non-local names bring more possibilities for name
collision
Namespaces allow us to group named entities that
otherwise would have global scope into narrower
scopes,
This allows organizing the elements of programs
into different logical scopes referred to by names
namespace identifier
{
named_entities
}
62. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
44/105
C++
Namespaces
Non-local names bring more possibilities for name
collision
Namespaces allow us to group named entities that
otherwise would have global scope into narrower
scopes,
This allows organizing the elements of programs
into different logical scopes referred to by names
namespace identifier
{
named_entities
}
63. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
45/105
C++
Namespaces
// namespaces
#include iostream
using namespace std;
namespace foo
{
int value() { return 5; }
}
namespace bar
{
const double pi = 3.1416;
double value() { return 2*pi; }
}
int main () {
cout foo::value() ’n’;
cout bar::value() ’n’;
cout bar::pi ’n’;
return 0;
}
64. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
46/105
C++
using function
The keyword using introduces a name into the current
declarative region (such as a block), thus avoiding the
need to qualify the name.
// using
#include iostream
using namespace std;
namespace first
{ int x = 5;
int y = 10; }
namespace second
{ double x = 3.1416;
double y = 2.7183;}
int main () {
using first::x;
using second::y;
cout x ’n’;
cout y ’n’;
cout first::y ’n’;
cout second::x ’n’;
return 0;
}
65. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
46/105
C++
using function
The keyword using introduces a name into the current
declarative region (such as a block), thus avoiding the
need to qualify the name.
// using
#include iostream
using namespace std;
namespace first
{ int x = 5;
int y = 10; }
namespace second
{ double x = 3.1416;
double y = 2.7183;}
int main () {
using first::x;
using second::y;
cout x ’n’;
cout y ’n’;
cout first::y ’n’;
cout second::x ’n’;
return 0;
}
66. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
47/105
C++
using namespace function
The keyword usingcan also be used as a directive to
introduce an entire namespace:
// using
#include iostream
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
cout x ’n’;
cout y ’n’;
cout second::x ’n’;
cout second::y ’n’;
return 0;
}
67. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Inline Function
Overloads
Templates
Name visibility
Namespaces
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
47/105
C++
using namespace function
The keyword usingcan also be used as a directive to
introduce an entire namespace:
// using
#include iostream
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
cout x ’n’;
cout y ’n’;
cout second::x ’n’;
cout second::y ’n’;
return 0;
}
68. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
48/105
C++
Array
An array is a series of elements of the same type
placed in contiguous memory locations.
An array containing 5 integer values of type int
called foo could be represented as:
An array must be declared before it is used. A
typical declaration for an array in C++ is:
type name[elements];
int foo[5] = {16, 2, 77, 40, 12071};
69. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
48/105
C++
Array
An array is a series of elements of the same type
placed in contiguous memory locations.
An array containing 5 integer values of type int
called foo could be represented as:
An array must be declared before it is used. A
typical declaration for an array in C++ is:
type name[elements];
int foo[5] = {16, 2, 77, 40, 12071};
70. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
48/105
C++
Array
An array is a series of elements of the same type
placed in contiguous memory locations.
An array containing 5 integer values of type int
called foo could be represented as:
An array must be declared before it is used. A
typical declaration for an array in C++ is:
type name[elements];
int foo[5] = {16, 2, 77, 40, 12071};
71. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
49/105
C++
Array
// arrays example
#include iostream
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n5 ; ++n )
{
result += foo[n];
}
cout result;
return 0;
}
72. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
50/105
C++
Multidimensional arrays
Multidimensional arrays can be described as
arrays of arrays
For example, a bidimensional array can be
imagined as a two-dimensional table with elements
of same uniform data type
The C++ syntax for this is:
int jimmy [3][5];
Multidimensional arrays are not limited to two
indices
char century [100][365][24][60][60];
Multidimensional arrays are just an abstraction for
programmers
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)
73. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
50/105
C++
Multidimensional arrays
Multidimensional arrays can be described as
arrays of arrays
For example, a bidimensional array can be
imagined as a two-dimensional table with elements
of same uniform data type
The C++ syntax for this is:
int jimmy [3][5];
Multidimensional arrays are not limited to two
indices
char century [100][365][24][60][60];
Multidimensional arrays are just an abstraction for
programmers
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)
74. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
50/105
C++
Multidimensional arrays
Multidimensional arrays can be described as
arrays of arrays
For example, a bidimensional array can be
imagined as a two-dimensional table with elements
of same uniform data type
The C++ syntax for this is:
int jimmy [3][5];
Multidimensional arrays are not limited to two
indices
char century [100][365][24][60][60];
Multidimensional arrays are just an abstraction for
programmers
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)
75. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
50/105
C++
Multidimensional arrays
Multidimensional arrays can be described as
arrays of arrays
For example, a bidimensional array can be
imagined as a two-dimensional table with elements
of same uniform data type
The C++ syntax for this is:
int jimmy [3][5];
Multidimensional arrays are not limited to two
indices
char century [100][365][24][60][60];
Multidimensional arrays are just an abstraction for
programmers
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)
76. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
50/105
C++
Multidimensional arrays
Multidimensional arrays can be described as
arrays of arrays
For example, a bidimensional array can be
imagined as a two-dimensional table with elements
of same uniform data type
The C++ syntax for this is:
int jimmy [3][5];
Multidimensional arrays are not limited to two
indices
char century [100][365][24][60][60];
Multidimensional arrays are just an abstraction for
programmers
int jimmy [3][5]; // is equivalent to
int jimmy [15]; // (3 * 5 = 15)
77. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
51/105
C++
Multidimensional array
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n][m]=(n+1)*(m+1);
}
}
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
}
Above (both) codes do not produce any output
But Assign values to the memory block called jimmy
in the following way
78. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
51/105
C++
Multidimensional array
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n][m]=(n+1)*(m+1);
}
}
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
}
Above (both) codes do not produce any output
But Assign values to the memory block called jimmy
in the following way
79. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
51/105
C++
Multidimensional array
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n][m]=(n+1)*(m+1);
}
}
#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()
{
for (n=0; nHEIGHT; n++)
for (m=0; mWIDTH; m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
}
Above (both) codes do not produce any output
But Assign values to the memory block called jimmy
in the following way
80. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
52/105
C++
Arrays as parameters
To pass an array to a function as a parameter
In C++, it is not possible to pass the entire block of
memory represented by an array to a function
directly as an argument
But what can be passed instead is its address. As it
is efficient way
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
81. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
52/105
C++
Arrays as parameters
To pass an array to a function as a parameter
In C++, it is not possible to pass the entire block of
memory represented by an array to a function
directly as an argument
But what can be passed instead is its address. As it
is efficient way
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
82. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
52/105
C++
Arrays as parameters
To pass an array to a function as a parameter
In C++, it is not possible to pass the entire block of
memory represented by an array to a function
directly as an argument
But what can be passed instead is its address. As it
is efficient way
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
83. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
52/105
C++
Arrays as parameters
To pass an array to a function as a parameter
In C++, it is not possible to pass the entire block of
memory represented by an array to a function
directly as an argument
But what can be passed instead is its address. As it
is efficient way
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
84. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
52/105
C++
Arrays as parameters
To pass an array to a function as a parameter
In C++, it is not possible to pass the entire block of
memory represented by an array to a function
directly as an argument
But what can be passed instead is its address. As it
is efficient way
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
85. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
53/105
C++
Arrays as parameters
// arrays as parameters
#include iostream
using namespace std;
void printarray (int arg[], int length) {
for (int n=0; nlength; ++n)
cout arg[n] ’ ’;
cout ’n’;
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
}
86. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
54/105
C++
Library arrays
The arrays explained above are directly
implemented as a language feature
Restricting its copy and easily decay into pointers,
they probably suffer from an excess of optimization
C++ provides an alternative array type as a
standard container (a type template (a class
template, in fact))
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
87. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
54/105
C++
Library arrays
The arrays explained above are directly
implemented as a language feature
Restricting its copy and easily decay into pointers,
they probably suffer from an excess of optimization
C++ provides an alternative array type as a
standard container (a type template (a class
template, in fact))
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
88. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
54/105
C++
Library arrays
The arrays explained above are directly
implemented as a language feature
Restricting its copy and easily decay into pointers,
they probably suffer from an excess of optimization
C++ provides an alternative array type as a
standard container (a type template (a class
template, in fact))
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
89. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
54/105
C++
Library arrays
The arrays explained above are directly
implemented as a language feature
Restricting its copy and easily decay into pointers,
they probably suffer from an excess of optimization
C++ provides an alternative array type as a
standard container (a type template (a class
template, in fact))
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
90. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
54/105
C++
Library arrays
The arrays explained above are directly
implemented as a language feature
Restricting its copy and easily decay into pointers,
they probably suffer from an excess of optimization
C++ provides an alternative array type as a
standard container (a type template (a class
template, in fact))
To accept an array as parameter for a function
The parameters can be declared as the array type,
but with empty brackets, omitting the actual size of
the array e.g.
void procedure (int arg[])
int myarray [40];
procedure (myarray);
91. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Array
Dynamic
Memory
Structure
Classes
File I/O
Codes
55/105
C++
language built-in array Vs container library array
#include iostream
using namespace std;
int main()
{
int myarray[3] = {10,20,30};
for (int i=0; i3; ++i)
++myarray[i];
for (int elem : myarray)
cout elem ’n’;
}
#include iostream
#include array
using namespace std;
int main()
{
arrayint,3 myarray {10,20,30};
for (int i=0; imyarray.size(); ++i)
++myarray[i];
for (int elem : myarray)
cout elem ’n’;
}
92. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
93. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
94. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
95. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
96. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
97. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
98. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
56/105
C++
Dynamic Memory
In previous programs all memory needs were
determined before program execution by defining
the variables
But when the memory needed depends on user
input.
On these cases, programs need to dynamically
allocate memory
Dynamic memory is allocated using operator new
new is followed by a data type specifier
If a sequence of more than one element is required,
the number of these within brackets [].
It returns a pointer to the beginning of the new block
of memory allocated.
pointer = new type
pointer = new type [number_of_elements]
int * foo;
foo = new int [5];
99. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
100. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
101. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
102. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
103. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
104. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
57/105
C++
Dynamic Memory
C++ provides two standard mechanisms to check if
the allocation was successful:
* One is by handling exceptions i.e.(exception of type
bad_alloc is thrown when the allocation fails.)
This exception method is the method used by
default by new
foo = new int [5];
* The other method is known as nothrow (the pointer
returned by new is a null pointer)
On failure, memory is not thrown over bad_alloc
foo = new (nothrow) int [5];
Failure is detected by null pointer
int * foo;
foo = new (nothrow) int [5];
if (foo == nullptr) {
// error assigning memory. Take measures.
}
105. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
58/105
C++
Erasing Pointers
Memory allocated dynamically is only needed
during specific periods of time within a program
To free memory allocated dynamically operator
delete is used
delete pointer;
delete[] pointer;
Note :
106. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
58/105
C++
Erasing Pointers
Memory allocated dynamically is only needed
during specific periods of time within a program
To free memory allocated dynamically operator
delete is used
delete pointer;
delete[] pointer;
Note :
107. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
58/105
C++
Erasing Pointers
Memory allocated dynamically is only needed
during specific periods of time within a program
To free memory allocated dynamically operator
delete is used
delete pointer;
delete[] pointer;
Note : The first statement releases the memory of a
single element allocated using new
108. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
58/105
C++
Erasing Pointers
Memory allocated dynamically is only needed
during specific periods of time within a program
To free memory allocated dynamically operator
delete is used
delete pointer;
delete[] pointer;
Note : The second one releases the memory allocated
for arrays of elements using new and a size in brackets
([]).
109. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Dynamic Memory
Structure
Classes
File I/O
Codes
59/105
C++
Dynamic Memory
// rememb-o-matic
#include iostream
#include new
using namespace std;
int main ()
{
int i,n;
int * p;
cout How many numbers would you like to type
? ;
cin i;
p= new (nothrow) int[i];
if (p == nullptr)
cout Error: memory could not be allocated
;
else
{
for (n=0; ni; n++)
{
cout Enter number: ;
cin p[n];
}
cout You have entered: ;
for (n=0; ni; n++)
cout p[n] , ;
delete[] p;
}
return 0;
}
110. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
60/105
C++
Structure
A data structure is a group of data elements
grouped together under one name
These data elements, known as members, can
have different types and different lengths
syntax is
struct type_name
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
object_names;
111. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
60/105
C++
Structure
A data structure is a group of data elements
grouped together under one name
These data elements, known as members, can
have different types and different lengths
syntax is
struct type_name
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
object_names;
112. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
60/105
C++
Structure
A data structure is a group of data elements
grouped together under one name
These data elements, known as members, can
have different types and different lengths
syntax is
struct type_name
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
object_names;
113. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
60/105
C++
Structure
A data structure is a group of data elements
grouped together under one name
These data elements, known as members, can
have different types and different lengths
syntax is
struct type_name
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
object_names;
114. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
61/105
C++
Structure
struct product {
int weight;
double price;
} ;
product apple;
product banana, melon;
struct product {
int weight;
double price;
} apple, banana, melon;
Note:
It is important to clearly differentiate between
what is the structure type name (product),
115. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
61/105
C++
Structure
struct product {
int weight;
double price;
} ;
product apple;
product banana, melon;
struct product {
int weight;
double price;
} apple, banana, melon;
Note:
It is important to clearly differentiate between
what is an object of this type (apple, banana, and
melon)
116. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
Codes
62/105
C++
Important Characters
Expression What is evaluated Equivalent
a.b Member b of object a
a-b Member b of object pointed to by a (*a).b
*a.b Value pointed to by member b of object a *(a.b)
118. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
119. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
120. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
121. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
122. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
123. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
64/105
C++
Classes
Classes are an expanded concept of data structures:
They can contain data members, but they can also
contain functions as members.
Classes are defined using either keyword class or
keyword struct, syntax is:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
...
object_names;
Classes have new things called access specifiers. And is
one of the following three keywords:private, public or
protected
Classes can be defined not only with keyword class, but
also with keywords struct and union.
124. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
65/105
C++
Access Specifiers
private members of a class are accessible only from
within other members of the same class (or from
their friends).
protected members are accessible from other
members of the same class (or from their friends),
but also from members of their derived classes.
Finally, public members are accessible from
anywhere where the object is visible.
By default, all members of a class declared with the
class keyword have private access for all its
members
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
125. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
65/105
C++
Access Specifiers
private members of a class are accessible only from
within other members of the same class (or from
their friends).
protected members are accessible from other
members of the same class (or from their friends),
but also from members of their derived classes.
Finally, public members are accessible from
anywhere where the object is visible.
By default, all members of a class declared with the
class keyword have private access for all its
members
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
126. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
65/105
C++
Access Specifiers
private members of a class are accessible only from
within other members of the same class (or from
their friends).
protected members are accessible from other
members of the same class (or from their friends),
but also from members of their derived classes.
Finally, public members are accessible from
anywhere where the object is visible.
By default, all members of a class declared with the
class keyword have private access for all its
members
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
127. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
65/105
C++
Access Specifiers
private members of a class are accessible only from
within other members of the same class (or from
their friends).
protected members are accessible from other
members of the same class (or from their friends),
but also from members of their derived classes.
Finally, public members are accessible from
anywhere where the object is visible.
By default, all members of a class declared with the
class keyword have private access for all its
members
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
128. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
66/105
Classes
// classes example
#include iostream
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect;
rect.set_values (3,4);
cout area: rect.area();
return 0;}
129. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
67/105
Classes
Multiple Type
The most important property of a class is that it is a
type
we can declare multiple objects of it.
For example, following with the previous example of
class Rectangle, we could have declared the object
rectb in addition to object rect:
130. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
67/105
Classes
Multiple Type
// example: one class, two objects
#include iostream
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area () {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
Rectangle rect, rectb;
rect.set_values (3,4);
rectb.set_values (5,6);
cout rect area: rect.area() endl;
cout rectb area: rectb.area() endl;
return 0;
}
131. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
68/105
Classes
Constructors
What would happen in the previous example if we
called the member function area before having
called set_values?
An undetermined result, since the members width
and height had never been assigned a value.
In order to avoid that, a class can include a special
function called its constructors
The Rectangle class above can easily be improved
by implementing a constructor
132. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
68/105
Classes
Constructors
What would happen in the previous example if we
called the member function area before having
called set_values?
An undetermined result, since the members width
and height had never been assigned a value.
In order to avoid that, a class can include a special
function called its constructors
The Rectangle class above can easily be improved
by implementing a constructor
133. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
68/105
Classes
Constructors
What would happen in the previous example if we
called the member function area before having
called set_values?
An undetermined result, since the members width
and height had never been assigned a value.
In order to avoid that, a class can include a special
function called its constructors
The Rectangle class above can easily be improved
by implementing a constructor
134. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
68/105
Classes
Constructors
What would happen in the previous example if we
called the member function area before having
called set_values?
An undetermined result, since the members width
and height had never been assigned a value.
In order to avoid that, a class can include a special
function called its constructors
The Rectangle class above can easily be improved
by implementing a constructor
135. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
69/105
Classes
Constructors
// example: class constructor
#include iostream
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int,int);
int area () {return (width*height);}
};
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb (5,6);
cout rect area: rect.area() endl;
cout rectb area: rectb.area() endl;
return 0;
}
136. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
70/105
Classes
Overloading Constructors
A constructor can also be overloaded with different
versions taking different parameters
The compiler will automatically call the one whose
parameters match the arguments
137. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
70/105
Classes
Overloading Constructors
A constructor can also be overloaded with a
different number of parameters and/or parameters
of different types
The compiler will automatically call the one whose
parameters match the arguments
138. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
71/105
Classes
Overloading Constructors
// overloading class constructors
#include iostream
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle ();
Rectangle (int,int);
int area (void) {return (width*height);}
};
Rectangle::Rectangle () {
width = 5;
height = 5;
}
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb;
cout rect area: rect.area() endl;
cout rectb area: rectb.area() endl;
return 0;
}
139. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
72/105
Classes
Uniform Initialization
The way of calling constructors by enclosing their
arguments in parentheses, as shown above, is
known as functional form
class_name object_name = initialization_value;
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout foo’s circumference: foo.circum()
’n’;
return 0;
}
140. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
72/105
Classes
Uniform Initialization
The way of calling constructors by enclosing their
arguments in parentheses, as shown above, is
known as functional form
class_name object_name = initialization_value;
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout foo’s circumference: foo.circum()
’n’;
return 0;
}
141. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
72/105
Classes
Uniform Initialization
The way of calling constructors by enclosing their
arguments in parentheses, as shown above, is
known as functional form
class_name object_name = initialization_value;
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout foo’s circumference: foo.circum()
’n’;
return 0;
}
142. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
72/105
Classes
Uniform Initialization
The way of calling constructors by enclosing their
arguments in parentheses, as shown above, is
known as functional form
class_name object_name = initialization_value;
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout foo’s circumference: foo.circum()
’n’;
return 0;
}
143. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
72/105
Classes
Uniform Initialization
The way of calling constructors by enclosing their
arguments in parentheses, as shown above, is
known as functional form
class_name object_name = initialization_value;
class_name object_name { value, value, value, ... }
// classes and uniform initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) { radius = r; }
double circum() {return 2*radius*3.14159265;}
};
int main () {
Circle foo (10.0); // functional form
Circle bar = 20.0; // assignment init.
Circle baz {30.0}; // uniform init.
Circle qux = {40.0}; // POD-like
cout foo’s circumference: foo.circum()
’n’;
return 0;
}
144. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
Introduction
Constructors
File I/O
Codes
73/105
Classes
Member initialization in constructors
// member initialization
#include iostream
using namespace std;
class Circle {
double radius;
public:
Circle(double r) : radius(r) { }
double area() {return radius*radius*3.14159265;}
};
class Cylinder {
Circle base;
double height;
public:
Cylinder(double r, double h) : base (r), height(h) {}
double volume() {return base.area() * height;}
};
int main () {
Cylinder foo (10,20);
cout foo’s volume: foo.volume() ’n’;
return 0;
}
145. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
74/105
File Input/Output
C++ provides the following classes to perform
output and input of characters to/from files
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
These classes are derived directly or indirectly from
the classes istream and ostream
// basic file operations
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile;
myfile.open (example.txt);
myfile Writing this to a file.n;
myfile.close();
return 0;
}
146. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
74/105
File Input/Output
C++ provides the following classes to perform
output and input of characters to/from files
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
These classes are derived directly or indirectly from
the classes istream and ostream
// basic file operations
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile;
myfile.open (example.txt);
myfile Writing this to a file.n;
myfile.close();
return 0;
}
147. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
74/105
File Input/Output
C++ provides the following classes to perform
output and input of characters to/from files
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
These classes are derived directly or indirectly from
the classes istream and ostream
// basic file operations
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile;
myfile.open (example.txt);
myfile Writing this to a file.n;
myfile.close();
return 0;
}
148. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
74/105
File Input/Output
C++ provides the following classes to perform
output and input of characters to/from files
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
These classes are derived directly or indirectly from
the classes istream and ostream
// basic file operations
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile;
myfile.open (example.txt);
myfile Writing this to a file.n;
myfile.close();
return 0;
}
149. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
74/105
File Input/Output
C++ provides the following classes to perform
output and input of characters to/from files
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.
These classes are derived directly or indirectly from
the classes istream and ostream
// basic file operations
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile;
myfile.open (example.txt);
myfile Writing this to a file.n;
myfile.close();
return 0;
}
150. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
75/105
File Input/Output
Open a file
In order to open a file with a stream object we use
its member function open:
open (filename, mode);
Filename is a string representing the name of the
file to be opened
Mode is an optional parameter with a combination
of the following flag
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
ios::ate Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.
ios::app All output operations are performed at the end of the file,
appending the content to the current content of the file.
ios::trunc If the file is opened for output operations and it already existed,
its previous content is deleted and replaced by the new one.
ofstream myfile;
myfile.open (example.bin, ios::out | ios::app |
ios::binary);
151. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
75/105
File Input/Output
Open a file
In order to open a file with a stream object we use
its member function open:
open (filename, mode);
Filename is a string representing the name of the
file to be opened
Mode is an optional parameter with a combination
of the following flag
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
ios::ate Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.
ios::app All output operations are performed at the end of the file,
appending the content to the current content of the file.
ios::trunc If the file is opened for output operations and it already existed,
its previous content is deleted and replaced by the new one.
ofstream myfile;
myfile.open (example.bin, ios::out | ios::app |
ios::binary);
152. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
75/105
File Input/Output
Open a file
In order to open a file with a stream object we use
its member function open:
open (filename, mode);
Filename is a string representing the name of the
file to be opened
Mode is an optional parameter with a combination
of the following flag
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
ios::ate Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.
ios::app All output operations are performed at the end of the file,
appending the content to the current content of the file.
ios::trunc If the file is opened for output operations and it already existed,
its previous content is deleted and replaced by the new one.
ofstream myfile;
myfile.open (example.bin, ios::out | ios::app |
ios::binary);
153. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
75/105
File Input/Output
Open a file
In order to open a file with a stream object we use
its member function open:
open (filename, mode);
Filename is a string representing the name of the
file to be opened
Mode is an optional parameter with a combination
of the following flag
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
ios::ate Set the initial position at the end of the file.
If this flag is not set, the initial position is the beginning of the file.
ios::app All output operations are performed at the end of the file,
appending the content to the current content of the file.
ios::trunc If the file is opened for output operations and it already existed,
its previous content is deleted and replaced by the new one.
ofstream myfile;
myfile.open (example.bin, ios::out | ios::app |
ios::binary);
154. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
76/105
File Input/Output
Open a file
classes ofstream, ifstream and fstream has a default mode that is used
if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
File streams opened in binary mode perform input and output
operations independently of any format considerations
Non-binary files are known as text files
ofstream myfile (example.bin, ios::out | ios
::app | ios::binary);
To check if a file stream was successful opening a file,use is_open
if (myfile.is_open()) { /* ok, proceed with
output */ }
Closing a file
myfile.close();
155. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
76/105
File Input/Output
Open a file
classes ofstream, ifstream and fstream has a default mode that is used
if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
File streams opened in binary mode perform input and output
operations independently of any format considerations
Non-binary files are known as text files
ofstream myfile (example.bin, ios::out | ios
::app | ios::binary);
To check if a file stream was successful opening a file,use is_open
if (myfile.is_open()) { /* ok, proceed with
output */ }
Closing a file
myfile.close();
156. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
76/105
File Input/Output
Open a file
classes ofstream, ifstream and fstream has a default mode that is used
if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
File streams opened in binary mode perform input and output
operations independently of any format considerations
Non-binary files are known as text files
ofstream myfile (example.bin, ios::out | ios
::app | ios::binary);
To check if a file stream was successful opening a file,use is_open
if (myfile.is_open()) { /* ok, proceed with
output */ }
Closing a file
myfile.close();
157. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
76/105
File Input/Output
Open a file
classes ofstream, ifstream and fstream has a default mode that is used
if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
File streams opened in binary mode perform input and output
operations independently of any format considerations
Non-binary files are known as text files
ofstream myfile (example.bin, ios::out | ios
::app | ios::binary);
To check if a file stream was successful opening a file,use is_open
if (myfile.is_open()) { /* ok, proceed with
output */ }
Closing a file
myfile.close();
158. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
76/105
File Input/Output
Open a file
classes ofstream, ifstream and fstream has a default mode that is used
if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
File streams opened in binary mode perform input and output
operations independently of any format considerations
Non-binary files are known as text files
ofstream myfile (example.bin, ios::out | ios
::app | ios::binary);
To check if a file stream was successful opening a file,use is_open
if (myfile.is_open()) { /* ok, proceed with
output */ }
Closing a file
myfile.close();
159. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
77/105
Text files
Writing
Writing operations on text files are performed in the
same way we operated with cout:
// writing on a text file
#include iostream
#include fstream
using namespace std;
int main () {
ofstream myfile (example.txt);
if (myfile.is_open())
{
myfile This is a line.n;
myfile This is another line.n;
myfile.close();
}
else cout Unable to open file;
return 0;
}
160. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
78/105
Text files
Reading
Reading from a file can also be performed in the same
way that we did with cin:
// reading a text file
#include iostream
#include fstream
#include string
using namespace std;
int main () {
string line;
ifstream myfile (example.txt);
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout line ’n’;
}
myfile.close();
}
else cout Unable to open file;
return 0;
}
161. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
79/105
File Input/Output
Checking state flags
The following member functions exist to check for specific states of a stream
(all of them return a bool value):
bad()
Returns true if a reading or writing operation fails. For example, in the
case that we try to write to a file that is not open for writing or if the
device where we try to write has no space left.
fail()
Returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is extracted
when we are trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end.
good()
It is the most generic state flag: it returns false in the same cases in
which calling any of the previous functions would return true. Note that
good and bad are not exact opposites (good checks more state flags at
once).
The member function clear() can be used to reset the state flags.
162. C/C++
Qazi Ejaz Ur Rehman
Avionics Engineer
History
Labs
Functions
Dynamic
Memory
Structure
Classes
File I/O
File I/O
fileopen
State flags
Binary
Codes
79/105
File Input/Output
Checking state flags
The following member functions exist to check for specific states of a stream
(all of them return a bool value):
bad()
Returns true if a reading or writing operation fails. For example, in the
case that we try to write to a file that is not open for writing or if the
device where we try to write has no space left.
fail()
Returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is extracted
when we are trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end.
good()
It is the most generic state flag: it returns false in the same cases in
which calling any of the previous functions would return true. Note that
good and bad are not exact opposites (good checks more state flags at
once).
The member function clear() can be used to reset the state flags.